一个与另一个实例的控件共享属性
本文关键字:实例 控件 共享 属性 另一个 一个 | 更新日期: 2023-09-27 18:20:49
我创建了自己的工具栏。在工具栏内部,我有集合属性来显示自定义项目:
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); }
}
在我的一个视图中,我声明了一个带有一个自定义项的工具栏:
<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>
基本上,我想在我的工具栏上放置自定义的"获取下一个数字"按钮。在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();
}
我的问题是,由于某种原因,我的应用程序(各种视图(中的所有工具栏都可以看到我仅在一个视图上声明的此自定义项。这显然会导致问题。我想知道我的代码有什么问题CustomItem
依赖项属性对整个应用程序变为静态?
答
依赖属性必须像这样声明:
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);
}
看起来您在 typeMetadata 参数 ( new PropertyMetadata(new List<UIElement>()
( 中为属性提供的默认值在所有实例之间共享 - 即它们都将以相同的空列表开始。使用 null 作为默认值,改为初始化构造函数中每个控件的空列表。