Reputation: 17905
I created my own toolbar. Inside toolbar I have collection property to display custom items:
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); }
}
On one of my views I declared toolbar with one custom item:
<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>
Basically, I wanted to place custom "Get next Number" button on my toolbar. Inside onApplyTemplate
I call this method:
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();
}
My problem is that for some reason ALL toolbars in my app (various views) see this custom item that I declared only on one view. This causing issues obviously. I wonder what is wrong with my code that CustomItem
dependency property becomes STATIC to whole APP?
ANSWER
Dependency property had to be declared like this:
public static readonly DependencyProperty CustomItemsProperty =
DependencyProperty.Register("CustomItems", typeof(List<UIElement>), typeof(DitatToolbar), new PropertyMetadata(null));
And I added initialization of this property to constructor:
public DitatToolbar()
{
this.CustomItems = new List<UIElement>();
this.DefaultStyleKey = typeof(DitatToolbar);
}
Upvotes: 1
Views: 115
Reputation: 2174
It looks like the default value that you have given the property in the typeMetadata parameter (new PropertyMetadata(new List<UIElement>()
) is shared across all instances - i.e they will all start off with the same empty list. Use null as the default value and instead initialise an empty list per control in the constructor.
Upvotes: 2