Thursday, November 14, 2013
Context Menu style must be applied to MenuItem Level by ItemContainerStyle to avoid delegate command bind to the same target
Context Menu style must be applied to MenuItem Level by ItemContainerStyle
Using TextBox.ContextMenu will create two instances of the style s1 so it will not bind only one textbox by Multiple binding.
<TextBox HorizontalAlignment="Left" Height="30" Margin="10,554,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="211" Name="tb1" >
<TextBox.ContextMenu>
<ContextMenu ItemsSource="{Binding Mode=OneWay,Path=SelectorData}" ItemContainerStyle="{StaticResource s1}"></ContextMenu>
</TextBox.ContextMenu>
</TextBox>
<Style TargetType="MenuItem" x:Key="s1">
<Setter Property="ItemsSource" Value="{Binding SubSelectorData}"></Setter>
<Setter Property="Header" Value="{Binding Name}"></Setter>
<Setter Property="Tag" Value="{Binding ReturnValue}"></Setter>
<Setter Property="Command" Value="{Binding SelectorCommand}"></Setter>
<Setter Property="CommandParameter">
<Setter.Value>
<MultiBinding Converter="{StaticResource multiSenderTarget2CmdParamConv}">
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type TextBox}}"></Binding>
<Binding RelativeSource="{RelativeSource Self}"></Binding>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
public List<SelectorData> SelectorData { get; set; }
SelectorService svc = new SelectorService();
svc.SelectorCommandAll = new DelegateCommand<object>((p) =>
{
Tuple<object, object> t = p as Tuple<object, object>;
TextBox tb = t.Item1 as TextBox;
MenuItem mi = t.Item2 as MenuItem;
tb.Text = mi.Tag.ToString() + ";" + mi.Header.ToString();
});
public class MutipleSenderTargetToCommandParameterConverter : System.Windows.Data.IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values.Length == 1) return new Tuple<object>(values[0]);
if (values.Length == 2) return new Tuple<object, object>(values[0], values[1]);
<Style x:Key="ssOnlyOneInstanceNotWorking" TargetType="{x:Type TextBox}" >
<Setter Property="TextBox.ContextMenu" >
<Setter.Value>
<ContextMenu ItemsSource="{Binding Mode=OneWay,Path=SelectorData}" ItemContainerStyle="{StaticResource s1}"></ContextMenu>
</Setter.Value>
</Setter>
</Style>
public class SelectorData
{
public string Name { get; set; }
public List<SelectorData> SubSelectorData { get; set; }
public bool HasSubSelectorData { get; set; }
public int Level { get; set; }
public DelegateCommand<object> SelectorCommand { get; set; }
public string ReturnValue { get; set; }
}
Subscribe to:
Posts (Atom)