Swi*_*ers 5 wpf xaml binding wpf-controls
<DataTemplate x:Key="_ItemTemplateA">
<Grid Tag="{Binding Path=DataContext.Command, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentControl Content="{Binding}" ContentTemplate="{StaticResource ContentTemplateB}" Grid.Row="0" />
<ContentControl Name="uiContentPresenter" Content="{Binding ContentView}" Grid.Row="1" Height="0" />
<ContentControl DataContext="{Binding IsContentDisplayed}" DataContextChanged="IsDisplayed_Changed" Visibility="Collapsed" />
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="Text"
Command="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"
CommandParameter="{Binding}" />
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
以上数据模板应用于ItemsControl.问题是,对于为Grid指定的ContextMenu,PlacementTarget属性实际上永远不会被设置为任何东西,所以我无法获得Grid的Tag属性,这是传递应该在父UserControl上执行的Command所必需的到上下文菜单.我基于这样的类似示例来建立这种方法:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0244fbb0-fd5f-4a03-bd7b-978d7cbe1be3/
我无法确定任何其他传递此命令的好方法.这是以这种方式设置的,因为我们正在使用MVVM方法,因此我们必须执行的命令存在于应用此模板的用户控件的视图模型中.我尝试以几种不同的方式显式设置PlacementTarget但它仍然总是显示为未设置.
我意识到这已经很旧并且得到了回答,但似乎没有得到正确的回答。我遇到了类似的帖子并留下了完整的答案。您可能想看一下,因为只需对代码进行一些调整就可以使其工作。
首先,说出你的观点UserControl……为了简单起见,我通常会说出我的所有观点This。然后记住我们的视图模型绑定到 的DataContext,UserControl我们可以使用 绑定到视图模型{Binding DataContext, ElementName=This}。
现在我们可以绑定到视图模型,我们必须将其与ContextMenu.DataContext. 我使用Tag对象的属性和ContextMenu(the PlacementTarget) 作为连接,在本例中为 a Grid:
<DataTemplate x:Key="YourTemplate" DataType="{x:Type DataTypes:YourDataType}">
<Grid ContextMenu="{StaticResource Menu}" Tag="{Binding DataContext,
ElementName=This}">
...
</Grid>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
ContextMenu然后,我们可以通过将属性绑定ContextMenu.DataContext到属性PlacementTarget.Tag(Grid在我们的示例中)来访问视图模型属性和命令:
<ContextMenu x:Key="Menu" DataContext="{Binding PlacementTarget.Tag, RelativeSource=
{RelativeSource Self}}">
<MenuItem Header="Delete" Command="{Binding DeleteFile}" CommandParameter=
"{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource
AncestorType=ContextMenu}}" CommandTarget="{Binding PlacementTarget,
RelativeSource={RelativeSource Self}}" />
</ContextMenu>
Run Code Online (Sandbox Code Playgroud)
请注意属性上的绑定MenuItem.CommandTarget。设置此项可确保引发指定命令的目标元素是PlacementTarget,或者Grid在本例中是 。
还要注意CommandParameter装订。DataContext这与的PlacementTarget或本例中的绑定Grid。的DataContext将从Grid继承,因此如果您正在使用该接口的某些实现,DataTemplate您的数据项现在将绑定到object您的参数:CommandICommand
public bool CanExecuteDeleteFileCommand(object parameter)
{
return ((YourDataType)parameter).IsInvalid;
}
public void ExecuteDeleteFileCommand(object parameter)
{
Delete((YourDataType)parameter);
}
Run Code Online (Sandbox Code Playgroud)
RelayCommand或者,如果您直接在视图模型中使用某种委托:
public ICommand Remove
{
get
{
return new ActionCommand(execute => Delete((YourDataType)execute),
canExecute => return ((YourDataType)canExecute).IsInvalid);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7175 次 |
| 最近记录: |