2012-01-18 9 views
1

自分のツールバーを作成しました。基本的にはコントロール共有のインスタンスが別のインスタンスとプロパティを共有していますか?

<my:DitatToolbar 
      Status="{Binding State, Converter={StaticResource ViewEditingStateToToolbarStateConverter}}" 
      Mode="DataEntry"> 
      <my:DitatToolbar.CustomItems> 
       <my:DitatToolbarButton Icon="/IDATT.Infrastructure.SL;component/Images/img_btn_calculate.png" Caption="Next&#x0d;&#x0a;Number" Index="6" Command="{Binding GetNextNumberCommand}" /> 
      </my:DitatToolbar.CustomItems> 
     </my:DitatToolbar> 

、私はカスタムの「次の番号を取得」を配置したい:私は1つのカスタム項目とツールバー宣言私のビューのいずれかに

public static readonly DependencyProperty CustomItemsProperty = 
      DependencyProperty.Register("CustomItems", typeof(List<UIElement>), typeof(DitatToolbar), new PropertyMetadata(new List<UIElement>())); 

     public List<UIElement> CustomItems 
     { 
      get { return GetValue(CustomItemsProperty) as List<UIElement>; } 
      set { this.SetValue(CustomItemsProperty, value); } 
     } 

:ツールバーの中に私は、カスタム項目を表示するには、コレクションプロパティを持っていますボタンをクリックします。 onApplyTemplate内部で私は、このメソッドを呼び出します。

internal void BuildUi() 
    { 

    if (this.ButtonsStackPanel == null) return; 

    this.defaultStatusVisibility = Visibility.Collapsed; 
    this.defaultNavigationVisibility = Visibility.Collapsed; 

    this.ButtonsStackPanel.Children.Clear(); 
    this.Items = new List<UIElement>(); 

    // Add buttons according to our work mode: 
    switch (this.Mode) 
    { 
     case ModeType.Ok: 
      this.Items.Add(this.GetNewButton(ButtonType.Ok)); 
      break; 

     case ModeType.OkCancel: 
      this.Items.Add(this.GetNewButton(ButtonType.Ok)); 
      this.Items.Add(this.GetNewButton(ButtonType.Cancel)); 
      break; 

     case ModeType.Lookup: 
      this.Items.Add(this.GetNewButton(ButtonType.CancelExit)); 
      this.Items.Add(this.GetNewButton(ButtonType.Ok)); 
      this.Items.Add(new DitatToolbarSeparator()); 
      this.Items.Add(this.GetNewButton(ButtonType.Refresh)); 
      break; 

     case ModeType.DataEntry: 
      this.defaultStatusVisibility = Visibility.Visible; 
      this.defaultNavigationVisibility = Visibility.Visible; 
      this.Items.Add(this.GetNewButton(ButtonType.CancelExit)); 
      this.Items.Add(this.GetNewButton(ButtonType.SaveExit)); 
      this.Items.Add(new DitatToolbarSeparator()); 
      this.Items.Add(this.GetNewButton(ButtonType.Cancel)); 
      this.Items.Add(this.GetNewButton(ButtonType.SaveClose)); 
      this.Items.Add(new DitatToolbarSeparator()); 
      this.Items.Add(this.GetNewButton(ButtonType.RenameId)); 
      this.Items.Add(this.GetNewButton(ButtonType.Delete)); 
      break; 

     case ModeType.OptionsDataEntry: 
      this.defaultStatusVisibility = Visibility.Visible; 
      this.Items.Add(this.GetNewButton(ButtonType.CancelExit)); 
      this.Items.Add(this.GetNewButton(ButtonType.SaveExit)); 
      this.Items.Add(new DitatToolbarSeparator()); 
      this.Items.Add(this.GetNewButton(ButtonType.Save)); 
      break; 

     default: 
      throw new NotSupportedException("DitatToolbar Mode have to be specified"); 
    } 

    if (this.Mode == ModeType.DataEntry || this.Mode == ModeType.OptionsDataEntry) 
    { 
     if (GetBindingExpression(CanEditProperty) == null) 
     { 
      this.SetBinding(CanEditProperty, new Binding("CanEdit") { Mode = BindingMode.TwoWay }); 
     } 

     if (GetBindingExpression(CanDeleteProperty) == null) 
     { 
      this.SetBinding(CanDeleteProperty, new Binding("CanDelete") { Mode = BindingMode.TwoWay }); 
     } 

     if (GetBindingExpression(CanRenameProperty) == null) 
     { 
      this.SetBinding(CanRenameProperty, new Binding("CanRename") { Mode = BindingMode.TwoWay }); 
     } 
    } 

    // Add custom buttons: 
    foreach (var customItem in this.CustomItems) 
    { 
     var ci = customItem as IToolbarItem; 
     this.Items.Insert(ci.Index, customItem); 
    } 

    // Insert buttons into container: 
    foreach (var element in this.Items) 
    { 
     this.ButtonsStackPanel.Children.Add(element); 
    } 

    // Navigation panel visibility 
    this.ShowNavigation(); 

    // Status panel visibility 
    this.ChangeStatus(); 
} 

私の問題は、何らかの理由で私のアプリですべてのツールバー(様々な見解が)私は1つのビューのみで宣言このカスタム項目を参照してくださいということです。これは明らかに問題を引き起こします。 CustomItem依存性プロパティがAPP全体に静的になるという私のコードで何が間違っているのだろうか?

ANSWER

依存関係プロパティは、このように宣言する必要がありました:

public static readonly DependencyProperty CustomItemsProperty = 
       DependencyProperty.Register("CustomItems", typeof(List<UIElement>), typeof(DitatToolbar), new PropertyMetadata(null)); 

そして、私はコンストラクタに、このプロパティの初期化を追加しました:

public DitatToolbar() 
     { 
      this.CustomItems = new List<UIElement>(); 

      this.DefaultStyleKey = typeof(DitatToolbar); 
     } 
+0

カスタムアイテムを持つ1つのビューからカスタムアイテムをコメントアウトすると、それは他のすべてのビューから消えますか? – arx

+0

それはそうではありません。今すぐ他のビューは、このUI要素の他のコンテナの子であることを示す実行時に例外を取得します。 – katit

答えて

2

これは、デフォルト値のように見えますtypeMetadataパラメータのプロパティ(new PropertyMetadata(new List<UIElement>())をすべてのインスタンスで共有していますつまり、それらはすべて同じ空のリストで始まります。 nullをデフォルト値として使用し、コンストラクタ内のコントロールごとに空のリストを初期化します。

+0

ビンゴ!ありがとうございました(それは私の答えを更新しました)(私はupvoteすることができましたが、何らかの理由でそれが答えとして取られない、後で試してみる) – katit

関連する問題