English 中文(简体)
C# :如何对项目数据类型投标?
原标题:C# : How to cast an object to ItemSource data type?

我正试图制定习惯用户控制。 在我的用户控制中,我使用两种控制清单箱和文本箱。 文本箱用于清单箱中的过滤项目。 为了做到这一点,我面临着我的过滤方法中的一个问题。 在我的过滤方法中,我需要对项目Source类型表示反对。 但是,我不理解我怎么能够这样做。 我在此试图:

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

calling :

   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

问题回答

你们想与项目(或点名项目)做的一切,都必须执行电子数据接口。 这就意味着,你可以通过旁听(简单地说)进行 basic。 因此,每个清单、阵列、收集、编辑等等。

通常,你可以把th锁的正常rou光拖出箱子!

因此, 目的确实是一种强硬(用破碎点打碎),你必须把它分开。

页: 1

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

never (except some rare unmeaningfull example where you explicitly return an empty string for ToStirng()) returns true because obj.ToString() is never null or Empty. In the standard implementation of the type Object (which every .net Type is based on) it returns the Namespace and the name of the type.

如果我很好地理解你的物体为testClas,而不是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;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签