控件共享属性的一个实例与另一个?

时间:2012-01-18 02:33:25

标签: c# silverlight xaml custom-controls

我创建了自己的工具栏。在工具栏中我有collection属性来显示自定义项:

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&#x0d;&#x0a;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依赖属性变成整个APP的STATIC?

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 个答案:

答案 0 :(得分:2)

看起来你在typeMetadata参数(new PropertyMetadata(new List<UIElement>())中给出属性的默认值是在所有实例之间共享的 - 即它们都将以相同的空列表开始。使用null作为默认值,而是在构造函数中为每个控件初始化一个空列表。