自分のツールバーを作成しました。基本的にはコントロール共有のインスタンスが別のインスタンスとプロパティを共有していますか?
<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
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);
}
カスタムアイテムを持つ1つのビューからカスタムアイテムをコメントアウトすると、それは他のすべてのビューから消えますか? – arx
それはそうではありません。今すぐ他のビューは、このUI要素の他のコンテナの子であることを示す実行時に例外を取得します。 – katit