WPF自定义控制重新初始化问题

时间:2011-01-08 19:21:38

标签: c# wpf wpf-controls color-picker

我正在开发一款时尚的ColorPicker Control并且效果很好,但我在重新初始化时遇到了问题。

我有一个叫做BaseBrushes的女巫看起来像这样

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

在XAML网站上,我将其设置为

<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>

现在我的问题是,每当我打开一个带有ColorPicker的窗口时,它会再次将所有画笔添加到List中,所以在第二个视图中我有14种颜色而不是7种颜色。

我可以在BeginInit()方法中清除List,但我认为这不是一个正确的解决方案。 我认为这种行为不正常,所以我没有看到什么。

如果有人知道某事,请帮助我

最诚挚的问候 迪马

1 个答案:

答案 0 :(得分:0)

这是因为在您的实施中,BaseBrushes的默认值是单ObservableCollection。重置尺寸仍然不是您想要的,因为所有ColorPickers仍然会共享相同的BaseBrushes集合。

您只需在BaseBrushes构造函数中为ColorPicker创建一个新的空集合:

BaseBrushes = new ObservableCollection<Brush>();

有关详细信息,请参阅本文中的编写集合属性部分:

相关问题