0

Trying to change one button to other, I have stuck in a problem with commands handling. It is difficult to describe its source, because nearly all are the same, but when I pass the command via setter it is not working.

My previous code (it is working like a charm): it is just a split button that passes its menu item header as parameter to executing command.

<xctk:SplitButton.DropDownContent>
    <ItemsControl ItemsSource="{Binding Instance.InstanceVM.Names}" x:Name="NamesCtr">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <MenuItem Header="{Binding}" Command="{Binding ElementName=NamesCtr, Path=DataContext.Instance.InstanceVM.Load}" CommandParameter="{Binding}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</xctk:SplitButton.DropDownContent> 

And here is for my dropdown button:

<controls:DropDownButton.DropDownContextMenu>
    <ContextMenu x:Name="NamesCtr" ItemsSource="{Binding Instance.InstanceVM.Names}">
        <ContextMenu.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="Template" Value="{StaticResource DropDownMenuItemTemplate}"/>
                <Setter Property="Header" Value="{Binding}"/>
                <Setter Property="Command" Value="{Binding ElementName=Namestr, Path=DataContext.Instance.InstanceVM.Load}"/>
                <Setter Property="CommandParameter" Value="{Binding}"/>
            </Style>
        </ContextMenu.ItemContainerStyle>
    </ContextMenu>
</controls:DropDownButton.DropDownContextMenu>

This shows a button, a dropdown with correct menu items (so the source, template and header were bound correctly, but the command does not hit its function)

Found a solution here : How do you bind a command to a MenuItem (WPF)?

Community
  • 1
  • 1
curiousity
  • 4,703
  • 8
  • 39
  • 59
  • possible duplicate of [How do you bind a command to a MenuItem (WPF)?](http://stackoverflow.com/questions/13826504/how-do-you-bind-a-command-to-a-menuitem-wpf) – poke Jul 21 '15 at 11:09

1 Answers1

0

As you are binding through a static singleton, you can modify the Binding like this:

<Setter Property="Command" Value="{Binding Source={x:Static <xmlnamespace>:<yourType>.Instance}, Path=InstanceVM.Load}"/

Don't forget to replace the placeholders with valid values, i.e. with an xml namespace prefix that corresponds to the appropriate CLR namespace and the type declaring the Instance property.

Please note that ElementName bindings and RelativeSource bindings are less trustworthy if placed in DataTemplates / Styles and also in case of using Popups in your visual tree.