【发布时间】:2012-06-08 13:50:03
【问题描述】:
我有一个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>
这不起作用,因为未找到 userControlName 的 DataContext。
我错过了什么吗?
【问题讨论】:
-
试试
PlacementTarget。 SO上有很多示例,例如stackoverflow.com/a/1022156/620360或stackoverflow.com/a/5357124/620360。
标签: wpf xaml data-binding user-controls command