[英]IList as dependency property, setter does not seem to be working
我有以下来自ComboBox
自定义用户控件:
public class EnumSourceComboBox : ComboBox
{
public static readonly DependencyProperty ExcludedItemsProperty =
DependencyProperty.Register(
"ExcludedItems",
typeof(IList),
typeof(EnumSourceComboBox),
new PropertyMetadata(
new List<object>()));
public EnumSourceComboBox()
{
this.ExcludedItems = new List<object>();
}
public IList ExcludedItems
{
get
{
return (IList)this.GetValue(EnumSourceComboBox.ExcludedItemsProperty);
}
set
{
this.SetValue(EnumSourceComboBox.ExcludedItemsProperty, value);
}
}
}
我在XAML
使用它的方式如下:
<controls:EnumSourceComboBox>
<controls:EnumSourceComboBox.ExcludedItems>
<employeeMasterData:TaxAtSourceCategory>FixedAmount</employeeMasterData:TaxAtSourceCategory>
<employeeMasterData:TaxAtSourceCategory>FixedPercentage</employeeMasterData:TaxAtSourceCategory>
</controls:EnumSourceComboBox.ExcludedItems>
</controls:EnumSourceComboBox>
这样可以构建良好,并且没有XAML
解析异常或类似的东西。 但是, ExcludedItems
始终具有count == 0
,因此XAML
中定义的两个项目不会添加到列表中。
如何定义依赖项属性以支持此行为?
更新 :这是TaxAtSourceCategory
枚举:
public enum TaxAtSourceCategory
{
/// <summary>
/// The none.
/// </summary>
None,
/// <summary>
/// The fixed amount.
/// </summary>
FixedAmount,
/// <summary>
/// The fixed percentage.
/// </summary>
FixedPercentage,
// Has some more values, but I don't think that matters
}
@cguedel,
我尝试了您的DependencyProperty代码,它运行正常。 我在属性的XAML内容中添加了两个字符串,而不是在TaxAtSourceCategory
实例中添加了两个字符串。在窗口的Loaded事件中,我对列表2进行了计数。
因此,也许您与TaxAtSourceCategory
类有关。 也许您可以显示TaxAtSourceCategory
代码,检查构造函数调用...
问候
在以下代码中,有一个EnumSourceCombobox,用于跟踪集合中的更改。
都跟踪:
-集合更改(集合被另一个替换)
-并更改集合(添加/删除/清除)
public class EnumSourceComboBox : ComboBox
{
private ObservableCollection<TaxAtSourceCategory> previousCollection;
public static readonly DependencyProperty ExcludedItemsProperty =
DependencyProperty.Register(
"ExcludedItems",
typeof(ObservableCollection<TaxAtSourceCategory>),
typeof(EnumSourceComboBox),
new PropertyMetadata(null));
public ObservableCollection<TaxAtSourceCategory> ExcludedItems
{
get
{
return (ObservableCollection<TaxAtSourceCategory>)this.GetValue(EnumSourceComboBox.ExcludedItemsProperty);
}
set
{
this.SetValue(EnumSourceComboBox.ExcludedItemsProperty, value);
}
}
public EnumSourceComboBox()
{
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(EnumSourceComboBox.ExcludedItemsProperty, typeof(EnumSourceComboBox));
dpd.AddValueChanged(this, (o, e) =>
{
if (previousCollection != null)
previousCollection.CollectionChanged -= ExcludedItemsChanged;
previousCollection = ExcludedItems;
if (previousCollection != null)
previousCollection.CollectionChanged += ExcludedItemsChanged;
});
this.ExcludedItems = new ObservableCollection<TaxAtSourceCategory>();
}
void ExcludedItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// Take into account change in the excluded items more seriously !
MessageBox.Show("CollectionChanged to count " + ExcludedItems.Count);
}
}
这是解决方案的链接: http : //1drv.ms/1P8cMVc
最佳使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.