【发布时间】:2012-05-01 00:05:06
【问题描述】:
我的视图 XAML 中有以下内容
<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>
Command 在 ViewModel 中定义
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和RelativeSource的TemplatedParent。我不认为你可以得到x:Name属性,但如果你能回到你的内容控件,那么也许你可以使用其他属性作为一种“标签”属性。
标签: wpf binding datatemplate