C#:如何将对象转换为ItemSource数据类型?

时间:2012-04-29 04:25:24

标签: c# wpf data-binding user-controls wpf-controls

我正在尝试开发自定义用户控件。在我的用户控件中,我使用两个控件,一个列表框和一个文本框。文本框用于过滤列表框中的项目。为此,我在我的过滤方法中遇到了问题。在我的filter方法中,我需要将对象强制转换为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

2 个答案:

答案 0 :(得分:0)

所有你想要为ItemSource(或者命名一致性的ItemsSource)提供的东西都必须实现IEnumerable接口。这意味着你可以遍历foreach(简单地说)。所以每个List,数组,集合,Dictionaries等。

Normaly你不能通过正常的开箱即用字符串迭代!

因此,如果您的Object真的是一个String(在调试时使用断点检查它),您必须以某种方式将其拆分。

ps:顺便说一下这段代码

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

never(除了为ToStirng()显式返回空字符串的一些罕见的无意义的示例),返回true,因为obj.ToString()永远不为null或为空。在Object类型的标准实现中(每个.net类型都基于它),它返回Namespace和类型的名称。

答案 1 :(得分: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;