Reputation: 1765
I got the following View of type MyCustomView:
<Grid>
...
<usercontrols:MyUsercontrol>
<usercontrols:MyUsercontrol.ContextMenu>
<ContextMenu>
<ContextMenu.ItemsSource>
<CompositeCollection>
<MenuItem IsEnabled="{Binding HOW_TO_BIND_TO_miTest}" Header="Test" Click="DoSomeAction" />
</CompositeCollection>
</ContextMenu.ItemsSource>
</ContextMenu>
</usercontrols:MyUsercontrol.ContextMenu>
</usercontrols:MyUsercontrol>
...
<Button Style="{StaticResource SidebarButton}" Click="OpenContextMenu" Content="Open Menu">
<Button.ContextMenu>
<ContextMenu>
<ContextMenu.ItemsSource>
<CompositeCollection>
<MenuItem x:Name="miTest" Header="Add Account" Click="DoSomeAction"/>
</CompositeCollection>
</ContextMenu.ItemsSource>
</ContextMenu>
</Button.ContextMenu>
</Button>
...
</Grid>
In the Code-Behind of the View I set the IsEnabled-Property of MenuItem "miTest" to false, so the MenuItem is disabled. How do I synchronise this with the IsEnabled-Property of the embedded UserControls' Contextmenu-Item? I already tried this one:
<MenuItem IsEnabled="{Binding Path=Test_binding.IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type MyCustomView}}}" Header="Test" Click="DoSomeAction" />
and added this Code-Behind:
public MenuItem Test_binding
{
get { return this.miTest; }
}
but this is not working.
Upvotes: 0
Views: 264
Reputation: 2665
Have a bool Property in your UserControl, Bind it to both of the MenuItem
in TwoWay
.. it will be Synchronised.
public bool IsEnabled
{
get { return isEnabled; }
set {
isEnabled = value;
PropertyChagned("IsEnabled");
}
}
Make sure the UserControl is derived with INotifyPropertyChagned
...
Bind this property with both MenuItem
...
<MenuItem IsEnabled="{Binding Path=IsEnabled, Mode=Twoway, RelativeSource={RelativeSource AncestorType={x:Type MyCustomView}}}" Header="Test" Click="DoSomeAction" />
Upvotes: 1