I have a UserControl
, defined like so:
我有一个UserControl,定义如下:
<UserControl x:Name=userControlName>
<UserControl.Resources>
<Style TargetType="{x:Type MyControl}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu >
<MenuItem Header="ITEM"
Command="{Binding ElementName=userControlName, Path=DeleteCommand">
</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controlType}">
<Grid>
<!--MyContentHere-->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<!--Content -->
<ListBox>
<ListBoxItem> <!--is of Type MyControl-->
</ListBox>
</Grid>
</UserControl>
This does not work, as the userControlName's DataContext
is not being found.
这不起作用,因为找不到userControlName的DataContext。
Am I missing something here?
我在这里错过了什么吗?
1 个解决方案
#1
0
You should try using a WPF RelativeSource
binding like such:
您应该尝试使用WPF RelativeSource绑定,如下所示:
<ContextMenu >
<MenuItem Header="ITEM"
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.DeleteCommand}">
</MenuItem>
</ContextMenu>
#1
0
You should try using a WPF RelativeSource
binding like such:
您应该尝试使用WPF RelativeSource绑定,如下所示:
<ContextMenu >
<MenuItem Header="ITEM"
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.DeleteCommand}">
</MenuItem>
</ContextMenu>