Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带IEnumerable的WPF从属属性<;对象>;_C#_Wpf - Fatal编程技术网

C# 带IEnumerable的WPF从属属性<;对象>;

C# 带IEnumerable的WPF从属属性<;对象>;,c#,wpf,C#,Wpf,我正在尝试使用编写自定义WPF控件 IEnumerable ItemSource objectselecteditem 字符串显示路径 作为依赖性属性 我无法将对象用作集合项类型。它不设置我的集合引用 当我将集合类型更改为隐式类型IEnumerable时,它工作正常 下面是我现在拥有的一些代码(不是全部,只是属性初始化和使用) public IEnumerable ItemsSource { get{return(IEnumerable)GetValue(ItemsSourcePropert

我正在尝试使用编写自定义WPF控件

  • IEnumerable ItemSource
  • objectselecteditem
  • 字符串显示路径
作为依赖性属性

我无法将
对象
用作集合项类型。它不设置我的集合引用

当我将集合类型更改为隐式类型
IEnumerable
时,它工作正常

下面是我现在拥有的一些代码(不是全部,只是属性初始化和使用)

public IEnumerable ItemsSource
{
get{return(IEnumerable)GetValue(ItemsSourceProperty);}
set{SetValue(ItemsSourceProperty,value);}
}
公共静态只读依赖项Property ItemsSourceProperty=
从属属性。寄存器(
“项目资源”,
类型(IEnumerable),
类型(自动完成组合框),
新的UIPropertyMetadata(空);
公共对象SelectedValue
{
获取{return(object)GetValue(SelectedValueProperty);}
set{SetValue(SelectedValueProperty,value);}
}
公共静态只读依赖属性SelectedValueProperty=
从属属性。寄存器(
“SelectedValue”,
类型(对象),
类型(自动完成组合框),
新的UIPropertyMetadata(空);
公共字符串显示路径
{
get{return(string)GetValue(DisplayPathProperty);}
set{SetValue(DisplayPathProperty,value);}
}
公共静态只读DependencyProperty DisplayPathProperty=
从属属性。寄存器(
“显示路径”,
类型(字符串),
类型(自动完成组合框),
新属性元数据(默认值(字符串));
我在使用ItemsSource(空引用)时有一个例外:

private void atb_TextChanged(对象发送方,textchangedventargs e)
{
如果(atb.Text.Length>0)
{
IEnumerable results=ItemsSource.Where(
委托(对象s)
{
var propertyInfo=s.GetType().GetProperty(DisplayPath);
var value=propertyInfo.GetValue(s,null);
var s1=字符串形式的值;
返回s1.ToLower().StartsWith(atb.Text.ToLower());
});
if(results.Any())
{
slb.ItemsSource=结果;
slb.Visibility=可见性.Visibility;
}
其他的
{
slb.Visibility=Visibility.Collapsed;
slb.ItemsSource=null;
}
}
其他的
{
slb.Visibility=Visibility.Collapsed;
slb.ItemsSource=null;
}
}

我将ObservaleCollection绑定到这个,再一次,当我将所有
对象
更改为
MyCustomItem

时,我终于找到了一个解决方案


我已将集合类型更改为
IEnumerable
(非泛型),在使用点,我必须调用
ItemsSource.OfType()
以使用Linq。

您是否尝试过ItemsSource=new collection?在构造函数中?我记得我必须这样做,如果我这样做了,它在一个地方也是空的,如果它是。这是一种自动神奇地设置为null的方法。在你有。。公共IEnumerable Items资源。。在该类的构造函数中,您必须这样做确保。。。我知道,但即使我在构造函数中实例化集合,在使用方法中也是空的。看来绑好以后就nulled@Tomasz使用
ItemSource
SelectedItem
DisplayMemberPath
进行控制,听起来很像
ListBox
。为什么不用呢?
public IEnumerable<object> ItemsSource
{
    get { return (IEnumerable<object>) GetValue(ItemsSourceProperty); }
    set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
    DependencyProperty.Register(
    "ItemsSource", 
    typeof (IEnumerable<object>), 
    typeof (AutoCompleteComboBox), 
    new UIPropertyMetadata(null));

public object SelectedValue
{
    get { return (object) GetValue(SelectedValueProperty); }
    set { SetValue(SelectedValueProperty, value); }
}
public static readonly DependencyProperty SelectedValueProperty =
    DependencyProperty.Register(
    "SelectedValue", 
    typeof (object), 
    typeof (AutoCompleteComboBox), 
    new UIPropertyMetadata(null));

public string DisplayPath
{
    get { return (string) GetValue(DisplayPathProperty); }
    set { SetValue(DisplayPathProperty, value); }
}
public static readonly DependencyProperty DisplayPathProperty = 
    DependencyProperty.Register(
    "DisplayPath", 
    typeof (string), 
    typeof (AutoCompleteComboBox), 
    new PropertyMetadata(default(string)));
private void atb_TextChanged(object sender, TextChangedEventArgs e)
{
    if (atb.Text.Length > 0)
    {
        IEnumerable<object> results = ItemsSource.Where(
            delegate(object s)
            {
                var propertyInfo = s.GetType().GetProperty(DisplayPath);
                var value = propertyInfo.GetValue(s, null);
                var s1 = value as string;
                return s1.ToLower().StartsWith(atb.Text.ToLower());
            });

        if (results.Any())
        {
            slb.ItemsSource = results;
            slb.Visibility = Visibility.Visible;
        }
        else
        {
            slb.Visibility = Visibility.Collapsed;
            slb.ItemsSource = null;
        }
    }
    else
    {
        slb.Visibility = Visibility.Collapsed;
        slb.ItemsSource = null;
    }
}