I have the following custom user control that derives from 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:
<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. However, the ExcludedItems
always have count == 0
, so the two items defined in XAML
are not added to the list.
How do you define a dependency property to support this behavior?
Update : Here's the TaxAtSourceCategory
enumeration:
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,
I tried your DependencyProperty code, and it runs fine. 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.
So may be you ve got Something to do with the TaxAtSourceCategory
class. And may be you could show the TaxAtSourceCategory
code, check the constructor call...
Regards
In the following code there is the EnumSourceCombobox with the tracking of the changes in the collection.
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
Best use
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.