Note that there are some explanatory texts on larger screens.

plurals
  1. POOne instance of control sharing property with another?
    text
    copied!<p>I created my own toolbar. Inside toolbar I have collection property to display custom items:</p> <pre><code>public static readonly DependencyProperty CustomItemsProperty = DependencyProperty.Register("CustomItems", typeof(List&lt;UIElement&gt;), typeof(DitatToolbar), new PropertyMetadata(new List&lt;UIElement&gt;())); public List&lt;UIElement&gt; CustomItems { get { return GetValue(CustomItemsProperty) as List&lt;UIElement&gt;; } set { this.SetValue(CustomItemsProperty, value); } } </code></pre> <p>On one of my views I declared toolbar with one custom item:</p> <pre><code>&lt;my:DitatToolbar Status="{Binding State, Converter={StaticResource ViewEditingStateToToolbarStateConverter}}" Mode="DataEntry"&gt; &lt;my:DitatToolbar.CustomItems&gt; &lt;my:DitatToolbarButton Icon="/IDATT.Infrastructure.SL;component/Images/img_btn_calculate.png" Caption="Next&amp;#x0d;&amp;#x0a;Number" Index="6" Command="{Binding GetNextNumberCommand}" /&gt; &lt;/my:DitatToolbar.CustomItems&gt; &lt;/my:DitatToolbar&gt; </code></pre> <p>Basically, I wanted to place custom "Get next Number" button on my toolbar. Inside <code>onApplyTemplate</code> I call this method:</p> <pre><code>internal void BuildUi() { if (this.ButtonsStackPanel == null) return; this.defaultStatusVisibility = Visibility.Collapsed; this.defaultNavigationVisibility = Visibility.Collapsed; this.ButtonsStackPanel.Children.Clear(); this.Items = new List&lt;UIElement&gt;(); // 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(); } </code></pre> <p>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 <code>CustomItem</code> dependency property becomes STATIC to whole APP?</p> <p><strong>ANSWER</strong></p> <p>Dependency property had to be declared like this:</p> <pre><code>public static readonly DependencyProperty CustomItemsProperty = DependencyProperty.Register("CustomItems", typeof(List&lt;UIElement&gt;), typeof(DitatToolbar), new PropertyMetadata(null)); </code></pre> <p>And I added initialization of this property to constructor:</p> <pre><code>public DitatToolbar() { this.CustomItems = new List&lt;UIElement&gt;(); this.DefaultStyleKey = typeof(DitatToolbar); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload