RizwanShk

Hi,

I am trying to bind a menu with a datasource, but if i try binding attached property of menuitem i am unable to succeed.

See the code below.

XAML Code:

Code Snippet

<Window.Resources>

<HierarchicalDataTemplate

x:Key="booksDataTemplate"

DataType='{x:Type sysXml:XmlNode}'

ItemsSource='{Binding Path=ChildNodes}'>

<TextBlock Text='{Binding Path=Value}'>

<TextBlock Text='{Binding XPath=@name}' />

</< SPAN>TextBlock>

</< SPAN>HierarchicalDataTemplate>

<XmlDataProvider x:Key="nodesDS" d:IsDataSource="True" Source="..\Quality_Data.xml"/>

</< SPAN>Window.Resources>

<Menu Background="#FFFFFFFF" x:Name="menu" VerticalAlignment="Top" Height="22" ItemsSource="{Binding Source={StaticResource nodesDS}, XPath=nodes/node/node}" ItemTemplate="{StaticResource booksDataTemplate}" FontSize="11" MenuItem.MouseEnter="CommonHandler"

MenuItem.IsCheckable="{Binding XPath=@IsCheckable}" MenuItem.IsChecked="{Binding XPath=@IsChecked}" MenuItem.StaysOpenOnClick="{Binding XPath=@StaysOpenOnclick}"/>

Datasource: Quality_Data.xml

Code Snippet

< xml version="1.0" encoding="utf-8" >

<nodes>

<node name="Study Quality" StaysOpenOnClick="True" IsCheckable="False" IsChecked="True">

<node name="Field of view" StaysOpenOnClick="True" IsCheckable="False" IsChecked="True">

<node name="Normal" StaysOpenOnClick="True" IsCheckable="False" IsChecked="True">

<node name="Normal-1" StaysOpenOnClick="True" IsCheckable="False" IsChecked="True">

<node name="Normal-11" StaysOpenOnClick="True" IsCheckable="True" IsChecked="False"/>

<node name="Normal-12" StaysOpenOnClick="True" IsCheckable="False" IsChecked="True">

<node name="Normal-12-a" StaysOpenOnClick="True" IsCheckable="True" IsChecked="True"/>

</node>

</node>

<node name="Normal-2" StaysOpenOnClick="True" IsCheckable="True" IsChecked="False"/>

<node name="Normal-3" StaysOpenOnClick="True" IsCheckable="True" IsChecked="False"/>

</node>

<node name="Inferior wall cut off" StaysOpenOnClick="True" IsCheckable="True" IsChecked="True"/>

<node name="Superior portion cut off" StaysOpenOnClick="True" IsCheckable="True" IsChecked="False"/>

<node name="Other" StaysOpenOnClick="True" IsCheckable="True" IsChecked="False"/>

</node>

</node>

</nodes>

Please help me find the correct way to do the needful.

Thanks,

RizwanShk



Re: Windows Presentation Foundation (WPF) Unable to bind IsCheckable, IsChecked and StayOpenOnClick properties of menuitem while binding menu with a datasource

Sam Bent - MSFT

You're setting MenuItem.IsCheckable on the Menu object, but you want to set it on the (generated) MenuItem objects. To do that, use ItemContainerStyle:

<Style x:Key="MyStyle" TargetType="{x:Type MenuItem}">

<Setter Property="IsCheckable" Value="{Binding XPath=@IsCheckable}"/>

</Style>

<Menu ... ItemContainerStyle="{StaticResource MyStyle}"... />

This applies the style to each generated MenuItem (even recursively), thus setting the IsCheckable property.