我见在我看来XAML如何将CommandParameter绑定到x:DataTemplate中父控件的名称?
<GroupBox Grid.Row="0" Header="Aktionen bei Prüfung Aufbau">
<ContentControl Content="{Binding BuildUpActions}" ContentTemplate="{StaticResource FullActionListTemplate}" x:Name="BuildUp"/>
</GroupBox>
<GroupBox Grid.Row="1" Header="Aktionen bei Prüfung Abbau">
<ContentControl Content="{Binding TearDownActions}" ContentTemplate="{StaticResource FullActionListTemplate}" x:Name="TearDown"/>
</GroupBox>
的DataTemplate中以下是在单独的资源
<DataTemplate x:Key="FullActionListTemplate">
<DockPanel LastChildFill="True">
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Neuer Ausgang" Style="{StaticResource ButtonRowStyle}"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=DataContext.NewFullIOCommand}"
CommandParameter="{Binding **?HOW?**}"
/>
<more buttons here...>
</StackPanel>
<ContentControl Content="{Binding}" >
</ContentControl>
</DockPanel>
</DataTemplate>
的命令是在视图模型定义的定义
public ICommand NewFullIOCommand
{
get
{
if (this._newFullIOCommand == null)
{
this._newFullIOCommand = new Mvvm.RelayCommand(parm => DoNewFullIO(parm));
} return this._newFullIOCommand;
}
}
我想成为能够确定2个列表中的哪一个生成命令。我想要一个CommandParameter传递给包含控件的x:Name的命令处理程序。
如何定义绑定?有没有更好的办法?
您可能想要查看使用'TemplateBinding'或使用'RelativeBinding'和'TemplatedParent'的'RelativeSource'。虽然我不认为你可以获得'x:Name'属性,但是如果你可以返回到你的内容控件,那么也许你可以使用其他属性作为一种“标签”属性。 – CodingGorilla 2012-04-19 13:13:21