Question

Im working on a stylish ColorPicker Control and it work good, but I have problems with reinitialization.

I have DP called BaseBrushes witch looks like this

public ObservableCollection<Brush> BaseBrushes
    {
        get { return (ObservableCollection<Brush>)GetValue(BaseBrushesProperty); }
        set { SetValue(BaseBrushesProperty, value); }
    }

    // Using a DependencyProperty as the backing store for BaseBrushes.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BaseBrushesProperty =
        DependencyProperty.Register("BaseBrushes", typeof(ObservableCollection<Brush>), typeof(ColorPicker), new UIPropertyMetadata(new ObservableCollection<Brush>()));

and on XAML site, I set it like this

<gc:ColorPicker Margin="0,15,0,0" SelectedBrush="#FF1E65C4" PaletteSize="6" StepSize="25">
   <gc:ColorPicker.BaseBrushes>
    <SolidColorBrush Color="Red"/>
    <SolidColorBrush Color="Blue"/>
    <SolidColorBrush Color="Orange"/>
    <SolidColorBrush Color="Green"/>
    <SolidColorBrush Color="Yellow"/>
    <SolidColorBrush Color="Black"/>
    <SolidColorBrush Color="White"/>
   </gc:ColorPicker.BaseBrushes>
  </gc:ColorPicker>

My problem is now, every time I open a window with that ColorPicker, it adds all the Brushes again to the List, so on second view I have 14 Colors and not 7.

I can clear the List in BeginInit() method, but I don't think it's a right solution. I think this behavior is not normal, so I don't see something.

Please if somebody knows somethign, help me

With Best Regards Dima

Was it helpful?

Solution

It is because in your implementation the default value of BaseBrushes is a single ObservableCollection. Resetting the size is still not what you want because all ColorPickers will still share the same BaseBrushes collection.

You can simply create a new empty collection for BaseBrushes in the ColorPicker constructor:

BaseBrushes = new ObservableCollection<Brush>();

See the section Writing Collection Properties in this article for more details:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top