Problema de reinicialización del control personalizado de WPF

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">
    <SolidColorBrush Color="Red"/>
    <SolidColorBrush Color="Blue"/>
    <SolidColorBrush Color="Orange"/>
    <SolidColorBrush Color="Green"/>
    <SolidColorBrush Color="Yellow"/>
    <SolidColorBrush Color="Black"/>
    <SolidColorBrush Color="White"/>

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

preguntado el 08 de enero de 11 a las 19:01

Where do you put the control? inside what when you use it? -

1 Respuestas

It is because in your implementation the default value of BaseBrushes es un soltero ObservableCollection. Resetting the size is still not what you want because all ColorPickers will still share the same BaseBrushes colección.

You can simply create a new empty collection for BaseBrushes en el objeto ColorPicker constructor:

BaseBrushes = new ObservableCollection<Brush>();

Ver la sección Writing Collection Properties in this article for more details:

Respondido el 09 de enero de 11 a las 00:01

Thank you very much, that was exact the problem. - Dimax

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.