2012-04-29 88 views
0

我想开发一个自定义用户控件。在我的用户控件中,我使用两个控件列表框和文本框。文本框用于过滤列表框中的项目。为此,我在过滤方法中遇到问题。在我的过滤器方法中,我需要将对象投射到ItemSource类型。但我不明白我该如何施展它。这是我的代码,我尝试:C#:如何将对象转换为ItemSource数据类型?

public static readonly DependencyProperty ItemSourceProperty = DependencyProperty.Register("ItemSourrce", typeof(IEnumerable), typeof(SSSearchListBox), new PropertyMetadata(new PropertyChangedCallback(OnItemsSourcePropertyChanged))); 

    private static void OnItemsSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
    { 
     var control = sender as SSSearchListBox; 
     if (control != null) 
      control.OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue); 

    } 

    private void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue) 
    { 
     // Remove handler for oldValue.CollectionChanged 
     var oldValueINotifyCollectionChanged = newValue as INotifyCollectionChanged; 

     if (null != oldValueINotifyCollectionChanged) 
     { 
      oldValueINotifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged); 
     } 
     // Add handler for newValue.CollectionChanged (if possible) 
     var newValueINotifyCollectionChanged = newValue as INotifyCollectionChanged; 
     if (null != newValueINotifyCollectionChanged) 
     { 
      newValueINotifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged); 
     } 
    } 

    void newValueINotifyCollectionChanged_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
    { 
     //Do your stuff here. 

    } 

    public IEnumerable ItemSourrce 
    { 
     get { return (IEnumerable)this.GetValue(ItemSourceProperty); } 
     set { this.SetValue(ItemSourceProperty, value); } 
    } 
public void TextFiltering(ICollectionView filteredview, DevExpress.Xpf.Editors.TextEdit textBox) 
    { 
     string filterText = ""; 
     filteredview.Filter = delegate(object obj) 
     { 

      if (String.IsNullOrEmpty(filterText)) 
      { 
       return true; 
      } 
      string str = obj as string; // Problem is here. 
             // I need to cast obj to my ItemSourrce Data Type. 
      if (String.IsNullOrEmpty(obj.ToString())) 
      { 
       return true; 
      } 

      int index = str.IndexOf(filterText, 0, StringComparison.InvariantCultureIgnoreCase); 
      return index > -1; 
     }; 
     textBox.EditValueChanging += delegate 
     { 
      filterText = textBox.Text; 
      filteredview.Refresh(); 
     }; 
    } 

    private void textEdit1_GotFocus(object sender, RoutedEventArgs e) 
    { 

     ICollectionView view = CollectionViewSource.GetDefaultView(ItemSourrce); 
     TextFiltering(view, textEdit1); 
    } 

调用该使用控制:

List<testClass> myList = new List<testClass>(); 
    public void testMethod() 
    { 
     for (int i = 0; i < 20; i++) 
     { 
      myList.Add(new testClass { testData=i.ToString()}); 
     } 
     myTestControl.ItemSourrce = myList; 
    } 

    public class testClass 
    { 
     public string testData { get; set; } 
    } 

thank`s

要assing到的ItemSource(或ItemsSource时命名的一致性)什么

回答

0

好一切必须实现IEnumerable接口。这意味着基本上你可以通过foreach进行迭代(简单地说)。所以每个List,数组,集合,字典等等。

通常你不能迭代通过一个正常的框字符串!

所以如果你的对象是一个字符串(在调试时用一个断点检查它),你必须以某种方式分割它。

PS:因为obj.ToString()是不能为null或空的方式验证码

if (String.IsNullOrEmpty(obj.ToString())) 
{ 
    return true; 
} 

从来没有(除了某些罕见unmeaningfull例如当你明确地返回一个空字符串ToStirng())返回true 。在Object类型的标准实现中(每个.NET类型都基于它),它返回名称空间和类型名称。

0

如果我理解得很好,您的物品的类型为testClass而不是string

所以你的代码应该是你试图做铸造的地方。

string str = string.Empty; 

testClass myObject = obj as testClass; 

if (myObject == null) 
    return true; 
else 
    str = myObject.testData; 

if (string.IsNullOrEmpty(str)) 
    return true; 

return str.IndexOf(filterText, 0, StringComparison.InvariantCultureIgnoreCase) > -1; 
+0

是的,我同意你的看法。 但是,如果我把myTestControl1.ItemSourrce = myList1; 和myTestControl2.ItemSourrce = myList2; 那么会发生什么?我想用我的控件,如列表框。 – 2012-04-29 06:03:24

+0

@ Hasan009只要两个ItemsSource都是testClass类型,它就会正常工作。因为您每次都将过滤器分配给特定的视图。对于你所有的观点来说,这不是同一个过滤器。 – Dummy01 2012-04-29 06:10:58

+0

其实我想要得到我的ItemSourrce的类型。在这种情况下,数据类型是testClass.I想要在几个项目中使用我的控件,因此数据源将发生更改。 – 2012-04-29 07:13:59

相关问题