Silverlight:telerik RadControls中RadGridView的一個Bug及解決辦法(轉載)

當RadGridView中嵌套RadComboBox,且RadGridView的高度不夠出現滾動條時,上下拉動滾動條後,RadComboBox中的選中值將丟失!

  以下圖:express

  滾動條未拖動前測試

  

 

  滾動條上下拖動後(注意下圖的高亮部分)this

  

 

  重現該Bug的測試代碼:spa

  Model層code

  TextValueObject.csorm

namespace RadControlsBug.Model
{
     public class TextValueObject
     {
         public string Text { set ; get ; }
  
         public string Value { set ; get ; }
     }
}

  SexType.csxml

namespace RadControlsBug.Model
{
     public class SexType
     {
         /// <summary>
         /// 男
         /// </summary>
         public static string Male = "男" ;
  
         /// <summary>
         /// 女
         /// </summary>
         public static string FeMale = "女" ;
     }
}

  SexTypeCollection.csblog

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開發,建議使用。

相關文章
相關標籤/搜索