Reputation: 69
I'm working with WPF and MVVM.
I created my dynamic Menu
two different ways, but both don't work.
First:
<DockPanel>
<Menu DockPanel.Dock="Top" Height="auto" ItemsSource="{Binding MeuPaudi}">
<Menu.Resources>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Command" Value="{Binding Path=MenuSelecionado}" />
<Setter Property="Header" Value="{Binding Texto}" />
<Setter Property="ItemsSource" Value="{Binding MenuFilhos}"/>
</Style>
</Menu.Resources>
</Menu>
</DockPanel>
Second:
<Menu ItemsSource="{Binding MeuPaudi}">
<Menu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Header" Value="{Binding Path=Texto}"/>
<Setter Property="IsCheckable" Value="{Binding Path=IsCheckable}"/>
<Setter Property="ItemsSource" Value="{Binding Path=MenuFilhos}"/>
<Setter Property="Command" Value="{Binding MenuSelecionado}" />
</Style>
</Menu.ItemContainerStyle>
</Menu>
I also create a Button
to test if the command works properly:
<Button Command="{Binding Path=MenuSelecionado}"/>
And it works. Can someone help me?
Upvotes: 0
Views: 1144
Reputation: 396
First you should ensure that your bindings are working fine. Quick way to check if your bindings are not working, is to add a dummy converter in your binding and put a breakpoint in Convert Method. If breakpoint is not hit means, means your binding is not getting fired i,e either path of property is not correct or data source is not attached.
Also you should remove Style:BasedOn. as this is not required. Following code should work.
<MenuItem Header="Main Menu" ItemsSource="{Binding MeuPaudi}">
<MenuItem.ItemContainerStyle>
<Style>
<Setter Property="MenuItem.Header" Value="{Binding Texto}"/>
<Setter Property="MenuItem.Command" Value="{Binding MenuSelecionado}"/>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
Upvotes: 1