7

I have a simple search text box. This text box acts as a filter. I've now copied/pasted the code for the 5th time and enough is enough. Time for a custom control.

left and right brackets have been replaced with ()

My custom control will be simple. My problem is I want to have a dependencyProperty on this control that is of type List(T).

I created a test project to proof it out and make sure it works. It works well. Ignore List.

Below is the entire class. The problem is that the only thing holding me up is replacing List (Person) with List(T). Something like List where: T is Object

typeof(List(T) where: T is Object) <= Obviously I can't do that but gives an idea what I'm trying to accomplish.

public class SearchTextBox : TextBox
{
    public static readonly DependencyProperty SourceProperty =
        DependencyProperty.Register("FilterSource", typeof(List<Person>), typeof(SearchTextBox), new UIPropertyMetadata(null)); //I WANT THIS TO BE LIST<T>

    public List<Person> FilterSource
    {
        get
        {
            return (List<Person>)GetValue(SourceProperty);
        }
        set
        {
            SetValue(SourceProperty, value);
        }
    }

    public static readonly DependencyProperty FilterPropertyNameProperty =
        DependencyProperty.Register("FilterPropertyName", typeof(String), typeof(SearchTextBox), new UIPropertyMetadata());

    public String FilterPropertyName
    {
        get
        {
            return (String)GetValue(FilterPropertyNameProperty);
        }
        set
        {
            SetValue(FilterPropertyNameProperty, value);
        }
    }

    public SearchTextBox()
    {
        this.KeyUp += new System.Windows.Input.KeyEventHandler(SearchBox_KeyUp);
    }

    void SearchBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        ICollectionView view = CollectionViewSource.GetDefaultView(FilterSource);

        view.Filter = null;
        view.Filter = new Predicate<object>(FilterTheSource);
    }

    bool FilterTheSource(object obj)
    {
        if (obj == null) return false;

        Type t = obj.GetType();

        PropertyInfo pi = t.GetProperty(FilterPropertyName);

        //object o = obj.GetType().GetProperty(FilterPropertyName);

        String propertyValue = obj.GetType().GetProperty(FilterPropertyName).GetValue(obj, null).ToString().ToLower();

        if (propertyValue.Contains(this.Text.ToLower()))
        {
            return true;
        }

        return false;
    }
}
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
tronious
  • 1,547
  • 2
  • 28
  • 45

1 Answers1

12

No; that's not possible.
Instead, just use the non-generic typeof(IList).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Does that solve my problem though? how would I declare the actual property? I can't declare Public IList<> FilterSource. Compiler error. It requires me to define what IList contains no? – tronious Dec 04 '12 at 18:44
  • 2
    if I do that the compilers tells me "Using the generic type 'System.Collections.Generic.IList' requires 1 argument public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("FilterSource", typeof(IList<>), typeof(SearchTextBox), new UIPropertyMetadata(null)); public IList FilterSource – tronious Dec 04 '12 at 18:47
  • 1
    Thank you very much for the answer. That worked perfect. Stackoverflow is great – tronious Dec 04 '12 at 18:50
  • where is the answer I can't figure it out – Mour_Ka May 19 '20 at 12:06
  • @tronious, what is an answer you talking about? – dellos Jul 30 '20 at 00:40