Items get added twice in navigation application
I have a custom Silverlight (4) control, not unlike a datagrid. This control has a property called ColumnConfiguration. When using this control in a Navigation application, the ColumnConfiguration has its items added again (through XAML), while retaining it's old columns, the result is that twice the normal amount of columns are added to the control.
public ColumnCollection ColumnConfiguration
{
get { return (ColumnCollection)GetValue(ColumnConfigurationProperty); }
set { SetValu开发者_如何学Pythone(ColumnConfigurationProperty, value); }
}
public class ColumnCollection : Collection<ColumnBase> { }
How can i prevent these columns being added to the control again?
Xaml is like this:
<my:Control.ColumnConfiguration>
<my:ColumnTypeA Width="*" MinWidth="50">
</my:ColumnTypeA>
<my:ColumnTypeB Width="2*" MinWidth="50">
</my:ColumnTypeB>
<my:ColumnTypeB Width="2*" MinWidth="50">
</my:ColumnTypeB>
<my:ColumnTypeB Width="*" MinWidth="50">
</my:ColumnTypeB>
<my:ColumnTypeB Width="2*" MinWidth="50">
</my:ColumnTypeB>
</my:Control.ColumnConfiguration>
Could it be that you supplied a default value in the DependencyProperty
UIPropertyMetaData? As the DependencyProperty
is static, the default value specified there will be the same (reference) for all instances of your control. This is harmless when using value types, but when your property is a reference type, you will have to set its initial value using the .ctor()
of your control (or any other instance-way) in order to create individual initial values.
I suppose you have this control twice instantiated in your application, supposedly using MVVM DataTemplate
? The first instance will add columns to the static ColumnCollection created using the default value and the secod will also use this very instance again.
You could verify this by looking at the default ToString() value of your ColumnCollection, it will contain a hash value for the reference.
To resolve, simply set the default value for the ColumnConfigurationProperty to null.
精彩评论