简体   繁体   English

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

[英]IList as dependency property, setter does not seem to be working

I have the following custom user control that derives from ComboBox : 我有以下来自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);
        }
    }
}

and I use it in XAML as follows: 我在XAML使用它的方式如下:

        <controls:EnumSourceComboBox>
            <controls:EnumSourceComboBox.ExcludedItems>
                <employeeMasterData:TaxAtSourceCategory>FixedAmount</employeeMasterData:TaxAtSourceCategory>
                <employeeMasterData:TaxAtSourceCategory>FixedPercentage</employeeMasterData:TaxAtSourceCategory>
            </controls:EnumSourceComboBox.ExcludedItems>
        </controls:EnumSourceComboBox>

This builds fine and there's no XAML parse exception or something similar. 这样可以构建良好,并且没有XAML解析异常或类似的东西。 However, the ExcludedItems always have count == 0 , so the two items defined in XAML are not added to the list. 但是, ExcludedItems始终具有count == 0 ,因此XAML中定义的两个项目不会添加到列表中。

How do you define a dependency property to support this behavior? 如何定义依赖项属性以支持此行为?

Update : Here's the TaxAtSourceCategory enumeration: 更新 :这是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, @cguedel,

I tried your DependencyProperty code, and it runs fine. 我尝试了您的DependencyProperty代码,它运行正常。 I added two strings in the XAML content of the property instead of TaxAtSourceCategory instances In the Loaded event of the window I had a count for the list of 2. 我在属性的XAML内容中添加了两个字符串,而不是在TaxAtSourceCategory实例中添加了两个字符串。在窗口的Loaded事件中,我对列表2进行了计数。

So may be you ve got Something to do with the TaxAtSourceCategory class. 因此,也许您与TaxAtSourceCategory类有关。 And may be you could show the TaxAtSourceCategory code, check the constructor call... 也许您可以显示TaxAtSourceCategory代码,检查构造函数调用...

Regards 问候

In the following code there is the EnumSourceCombobox with the tracking of the changes in the collection. 在以下代码中,有一个EnumSourceCombobox,用于跟踪集合中的更改。
There are both tracking of : 都跟踪:
-the collection change (collection replaced with another on) -集合更改(集合被另一个替换)
-and changes in the collection ( add/remove/clear) -并更改集合(添加/删除/清除)

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);
    }
}

Here is a link to the solution : http://1drv.ms/1P8cMVc 这是解决方案的链接: http : //1drv.ms/1P8cMVc

Best use 最佳使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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