2011-01-08 139 views
1

我在一個時尚的ColorPicker控件上工作,它工作良好,但我有重新初始化的問題。WPF自定義控件重新初始化問題

我有DP稱爲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,它將所有刷子再次添加到列表中,所以在第二個視圖中,我有14種顏色而不是7。 我認爲這種行爲是不正常的,所以我沒有看到什麼。

請如果有人知道somethign,幫我

與問候 迪馬

+0

你把控制權放在哪裏?裏面什麼時候使用它? – Homam 2011-01-08 21:29:33

回答

0

這是因爲在你的實現BaseBrushes的默認值是一個ObservableCollection。重置尺寸仍然不是你想要的,因爲所有的ColorPickers仍然會共享相同的BaseBrushes集合。

你可以簡單地在ColorPicker構造函數創建一個新的空收集BaseBrushes

BaseBrushes = new ObservableCollection<Brush>(); 

請參閱本文的詳細信息,在部分寫作集合屬性

+0

非常感謝,這確實是問題所在。 – Dimax 2011-01-10 10:29:27