Skip to main content
 首页 » 编程设计

c#之将 Button 绑定(bind)到 ItemsControl 容器中的 RelayCommand

2024年02月13日15xiaohuochai

在 WPF 应用程序中,我有一个 ViewModel,它公开了一组字符串,我使用 WrapPanel 通过 ItemsControl 容器将这些字符串显示为按钮。但是,我无法将 ViewModel 中的 RelayCommand 绑定(bind)到按钮。

View 模型(事件地址 View 模型):

public IEnumerable<string> Addresses { get; set; } 
 
public RelayCommand<string> ZoomToAddressCommand { get { 
  if (this.zoomToAddressCommand == null) this.zoomToAddressComamnd = new RelayCommand<string>(this.ZoomToAddress); 
  return this.zoomToAddressCommand; 
}} 
 
private void ZoomToAddress(string address) { MessageBox.Show (address); } 

XAML:

<TabItem x:Name="IncidentAddressesTab"> 
  <ItemsControl ItemsSource="{Binding Addresses}"> 
    <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Command"> 
        <cmd:EventToCommand  
          Command="{Binding ZoomToAddressCommand}" 
          CommandParameter="{Binding Text}" 
         PassEventArgsToCommand="True"  
        /> 
      </i:EventTrigger> 
    </i:Interaction.Triggers> 
 
    <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
        <WrapPanel Orientation="Vertical"/> 
      </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
      <DataTemplate> 
        <Button Content="{Binding}" /> 
      </DataTemplate> 
    </ItemsControl.ItemTemplate> 
  </ItemsControl> 
</TabItem> 

连接 DataContext 的 Xaml 代码隐藏

IncidentAddressesTab.DataContext = new IncidentAddressesViewModel(); 

按钮显示地址。当我在 ZoomToAddressCommand 设置断点时,它确实被击中一次,但是当我单击按钮时,ZoomToAddress 方法永远不会被调用。

更新以包含绑定(bind)详细信息: 我实际上绑定(bind)到 TabItem。我更新了 XAML 以包含附加标记并在 XAML 代码隐藏中添加了绑定(bind)代码。我不知道这是相关信息,否则我会在开始时添加它.. (:

请您参考如下方法:

它不起作用,因为您已尝试将 Command Bind 到 ItemsControl 而不是 Button 控制。你试过这个吗?:

<DataTemplate> 
    <Button Content="{Binding}" Command="{Binding DataContext.ZoomToAddressCommand,  
        RelativeSource={RelativeSource AncestorType={x:Type  
        YourViewNamespace:YourViewName}}}" /> 
</DataTemplate> 

我们在这里尝试做的是从DataTemplateBind到我假设设置为DataContext的 View 模型> 当前 View 。请将“YourViewNamespace:YourViewName”替换为您的 XML 命名空间和 View 的实际名称。

更新>>>

好的,再次查看您的代码后,我可以看到您只是使用属性名称绑定(bind) 到Addresses 集合。你说 DataContext 是在 ItemsControl 上设置的,所以我假设你的意思是你的 View 模型是在 ItemsControl.DataContext< 上设置的 属性。如果是这样,那么我们需要将 Binding 更改为 Command,如下所示:

<DataTemplate> 
    <Button Content="{Binding}" Command="{Binding DataContext.ZoomToAddressCommand,  
        RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" /> 
</DataTemplate> 

如果您的 View 模型未在 ItemsControl.DataContext 属性上设置,那么这将不起作用,您需要清楚地告诉我您是如何将 View 模型连接到 View 的。在我假设您的 View 模型是数据绑定(bind)到包含 View 或 Window 的 DataContext 之前,通常是这样做的......也许下次,您可以提供此信息在您的问题中,让人们更容易回答?

更新 2 >>>

好的,您已经使用基本的 DataContext 信息更新了问题……完美。现在我可以在没有所有猜测的情况下正确地回答你的问题......你是否看到如果你首先在那里添加它会有多容易?没关系……我们现在在这里。试试这个最后的例子:

<DataTemplate> 
    <Button Content="{Binding}" Command="{Binding DataContext.ZoomToAddressCommand,  
        RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}" /> 
</DataTemplate> 

重申...此RelativeSource Binding 将查找可视化树,直到找到TabItem 控件。然后,它将查看该控件的 DataContext 属性。最后,它将在设置为 TabItem 的 DataContext 的对象(您的 View 模型)中查找 ZoomToAddressCommand 属性...我们到了。