繁体   English   中英

IList作为依赖项属性,setter似乎不起作用

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM