silverlight:telerik RadControls中RadGridView的一个Bug及解决办法
当RadGridView中嵌套RadComboBox,且RadGridView的高度不够出现滚动条时,上下拉动滚动条后,RadComboBox中的选中值将丢失!
如下图:
滚动条未拖动前
滚动条上下拖动后(注意下图的高亮部分)
重现该Bug的测试代码:
Model层
TextValueObject.cs
namespace RadControlsBug.Model { public class TextValueObject { public string Text { set; get; } public string Value { set; get; } } }
SexType.cs
namespace RadControlsBug.Model { public class SexType { /// <summary> /// 男 /// </summary> public static string Male = "男"; /// <summary> /// 女 /// </summary> public static string FeMale = "女"; } }
SexTypeCollection.cs
using System.Collections.Generic; namespace RadControlsBug.Model { public static class SexTypeCollection { private static List<TextValueObject> _items = new List<TextValueObject>(); public static List<TextValueObject> Items { get { return _items; } set { _items = value; } } static SexTypeCollection() { _items.Add(new TextValueObject() { Text = "男", Value = SexType.Male }); _items.Add(new TextValueObject() { Text = "女", Value = SexType.FeMale }); } } }
Person.cs
using System.Collections.Generic; namespace RadControlsBug.Model { public class Person { public string Name { set; get; } public string Sex { set; get; } private List<TextValueObject> _sexItems = SexTypeCollection.Items; public List<TextValueObject> SexItems { get { return _sexItems; } } } }
Company.cs
using System.Collections.ObjectModel; namespace RadControlsBug.Model { public class Company { private ObservableCollection<Person> _employees = new ObservableCollection<Person>(); public ObservableCollection<Person> Employees { get { return _employees; } set { _employees = value; } } public Company() { this._employees.Add(new Person() { Name = "张三", Sex = SexType.Male }); this._employees.Add(new Person() { Name = "李四", Sex = SexType.FeMale }); this._employees.Add(new Person() { Name = "王五", Sex = SexType.Male }); this._employees.Add(new Person() { Name = "赵六", Sex = SexType.FeMale }); this._employees.Add(new Person() { Name = "孙七", Sex = SexType.Male }); this._employees.Add(new Person() { Name = "杨九", Sex = SexType.FeMale }); this._employees.Add(new Person() { Name = "胡十", Sex = SexType.Male }); } } }
UI层:
MainPage.Xaml:
<UserControl x:Class="RadControlsBug.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <telerik:RadGridView ShowGroupPanel="False" RowIndicatorVisibility="Collapsed" CanUserFreezeColumns="False" AutoGenerateColumns="False" ItemsSource="{Binding Employees,Mode=TwoWay}" Width="300" Height="120" Name="gridView1"> <telerik:RadGridView.Columns> <telerik:GridViewColumn Header="姓名" Width="80" > <telerik:GridViewColumn.CellTemplate> <DataTemplate> <telerik:RadMaskedTextBox Value="{Binding Name,Mode=TwoWay}" MaskType="None"></telerik:RadMaskedTextBox> </DataTemplate> </telerik:GridViewColumn.CellTemplate> </telerik:GridViewColumn> <telerik:GridViewColumn Header="性别" Width="80" > <telerik:GridViewColumn.CellTemplate> <DataTemplate> <telerik:RadComboBox ItemsSource="{Binding SexItems,Mode=TwoWay}" SelectedValue="{Binding Sex,Mode=TwoWay}" SelectedValuePath="Value" DisplayMemberPath="Text"/> </DataTemplate> </telerik:GridViewColumn.CellTemplate> </telerik:GridViewColumn> </telerik:RadGridView.Columns> </telerik:RadGridView> </StackPanel> </Grid> </UserControl>
MainPage.Xaml.cs:
using System.Windows; using System.Windows.Controls; using RadControlsBug.Model; namespace RadControlsBug { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Page_Loaded); } void Page_Loaded(object sender, RoutedEventArgs e) { Company _viewModel = new Company(); this.DataContext = _viewModel; } } }
该问题曾经困扰我长达2周之久,在Telerik的论坛上提问也未得到回复。
曾经反复尝试,发现解决方法居然极其简单:
<telerik:RadComboBox ItemsSource="{Binding SexItems,Mode=TwoWay}" SelectedValue="{Binding Sex,Mode=TwoWay}" SelectedValuePath="Value" DisplayMemberPath="Text"/>
改成:
<telerik:RadComboBox SelectedValue="{Binding Sex,Mode=TwoWay}" SelectedValuePath="Value" DisplayMemberPath="Text" ItemsSource="{Binding SexItems,Mode=TwoWay}"/>
后,问题奇迹般的解决了!
分享于此,希望有助于遇到同样问题的朋友。(个人分析:有可能telerik的开发人员在解析XAML时,判断逻辑依赖于属性出现的顺序导致--胡猜的,我也没去看它的源码)
最后谈一下我个人对于Telerik RadControls For Silverlight这套控件的感受,用这套控件做项目开发已经有近3个月的时间,总体感觉还不错,能大幅提高团队的开发效率,官方有详细文档和示例,上手非常容易,而且客观来讲,BUG也比较少(用了3个月,基本上才发现这一个比较诡异的BUG),此外,如果是正版用户,官方还提供源码,并有一年的免费升级期限,每季度官方均会对整套控件做一次升级(主要是修复之前的BUG,以及增加一些新功能)。 从成本上考虑,一套控件的售价9k RMB左右(无Licence数量限制,而且能拿到源码任意修改),国内用户可在慧都控件网上直接购买,对于公司来讲这个成本其实并不高(相比公司招人自己实现这些控件的功能而言,9k多其实可以忽略不计了),如果您的公司打算致力于企业级应用的RIA开发,建议使用。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。