数据将列表绑定到用户控件依赖项属性 [英] Data Binding a list to a user control dependency property
问题描述
我没有找到我做错的事情。我创建了一个具有List< string>的用户控件。对于依赖属性。当我将数据绑定到该属性时,列表中没有任何内容。我使用相同的数据绑定到ComboBox,它显示列表就好了。
这是我的依赖属性:
I am having no luck finding out what I am doing wrong. I made a user control that has a List<string> for a dependency property. When I data bind to that property, nothing is in the list. I used the same data to bind to a ComboBox and it shows the list just fine.
Here is my dependency property:
public List<string> ValueList
{
get { return (List<string>)GetValue(ValueListProperty); }
set { SetValue(ValueListProperty, value); }
}
// Using a DependencyProperty as the backing store for ValueList. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ValueListProperty =
DependencyProperty.Register("ValueList", typeof(List<string>),
typeof(ValueBox),
new FrameworkPropertyMetadata
{
DefaultValue = new List<string>(),
DefaultUpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged,
});
这是我想要绑定的数据对象:
Here is my data object that I am trying to bind:
private ObservableCollection<string> _valueList1 = new ObservableCollection<string>();
/// <summary>
/// List of items for Valueboxes that display a list of items.
/// </summary>
public ObservableCollection<string> ValueList1
{
get
{
return _valueList1;
}
set
{
if (value != _valueList1)
{
_valueList1 = value;
NotifyPropertyChanged("ValueList1");
}
}
}
它会在后面的代码中填充多个值,我注意到这些值没有触发Set,但是它与组合框一起使用所以我假设这不是问题。
这是控件的XAML。 ComboBox工作,我的控件在ValueList属性中看不到任何内容:
It gets populated in the code behind with several values, which I noticed does not fire the Set, but it works with the combobox so I assume that is not the problem.
Here is the XAML for the control. The ComboBox works, my control does not see anything in the ValueList property:
<ComboBox Width="120"
ItemsSource="{Binding ValueList1}"
Text="{Binding Label1}"
IsEditable="True"/>
<uc:UserControl Value="Test"
ValueType="OPTION"
ValueList="{Binding ValueList1}"/>
现在,如果我将数据放入XAML中的控件中,它可以正常工作:
Now if I put the data in the control in the XAML, it works fine:
<uc:UserControl Value="Yes"
ValueType="OPTION">
<uc:UserControl.ValueList>
<System:String>No</System:String>
<System:String>Yes</System:String>
</uc:UserControl.ValueList>
</uc:UserControl>
推荐答案
依赖项属性(List< string>)类型不匹配和数据对象(ObservableCollection< string>)。
仍然不确定为什么组合框可以用这个并且我的控制权除非有内置的在转换器中用于组合框。
There is a mismatch between the dependency property (List<string>) type and the data object (ObservableCollection<string>).
Still not sure why the combobox was ok with this and my control wasn''t unless there is a built-in converter for the combobox.
这篇关于数据将列表绑定到用户控件依赖项属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!