0

カスタム ユーザー コントロールを開発しようとしています。私のユーザー コントロールでは、リスト ボックスとテキスト ボックスの 2 つのコントロールを使用しています。テキスト ボックスは、リスト ボックス内の項目をフィルター処理するために使用されます。これを行うには、フィルター メソッドで問題に直面しています。私のフィルタ メソッドでは、オブジェクトを 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);
    }

この Use Control を呼び出します:

   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; }
    }

ありがとう

4

2 に答える 2

0

ItemSource (または命名の一貫性のために ItemsSource) に割り当てたいものはすべて、IEnumerable インターフェイスを実装する必要があります。Thist は、基本的に foreach を介して反復できるすべてのものを意味します (簡単に言えば)。したがって、すべてのリスト、配列、コレクション、辞書など。

通常、通常のボックス文字列を反復処理することはできません!

したがって、オブジェクトが実際に文字列である場合 (デバッグ時にブレークポイントで確認してください)、何らかの方法で分割する必要があります。

ps:ちなみにこのコード

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

決して (ToStirng() に対して空の文字列を明示的に返すいくつかのまれな無意味な例を除いて) true を返します。タイプ オブジェクト (すべての .net タイプが基づいている) の標準実装では、名前空間とタイプの名前を返します。

于 2012-04-29T05:31:17.150 に答える
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;
于 2012-04-29T05:53:29.253 に答える