【问题标题】:WPF Binding Button.Command in ControlTemplate to property of ViewModelWPF 将 ControlTemplate 中的 Button.Command 绑定到 ViewModel 的属性
【发布时间】:2010-08-11 22:03:57
【问题描述】:

我的 ViewModel 有一个名为 Commands 的属性,它的类型是 IDictionary。 对于我的数据网格,我使用如下按钮为其中一个字段创建了一个 ControlTemplate:

 <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
         <Button Style="{DynamicResource btnRemove}" Width="14" Height="14" 
                                Content="{TemplateBinding Content} " 
                                CommandParameter="{Binding ViewID}"
                                 Command="{Binding Commands[AcknowledgeErrorCmd]}" />
                     <ControlTemplate.Triggers>
                     </ControlTemplate.Triggers>
                </ControlTemplate>

单击该按钮不会告诉我绑定不起作用。但是,添加到托管此网格的同一窗口的工具栏的无样式按钮可以正常工作,并正确绑定到此命令。我想我的问题是:

如何将 ControlTemplate 中使用的按钮的命令属性绑定到 ViewModel?

TIA。

【问题讨论】:

    标签: binding


    【解决方案1】:

    我不确定问题是什么,但尝试调试您的解决方案并查看输出窗口并在组合框中选择了“调试”,您将看到绑定期间发生的错误。也许这将帮助您解决问题。 如果您不理解,请将错误作为对此帖子的评论提供给我。

    【讨论】:

      【解决方案2】:

      我这样做了:

      <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">                                        
                          <Border  >
                              <TextBlock Margin="5">
                                      <Hyperlink  
                                          CommandParameter="{Binding ElementName=root, Path=DataContext.ViewID}"
                                          Command="{Binding ElementName=root, Path=DataContext.Commands[AcknowledgeErrorCmd]}">
                                          <TextBlock Text="Acknowledge"/>                                   
                                      </Hyperlink>                                                                                
                                  </TextBlock>
                          </Border>                   
                      </ControlTemplate>
      

      而且效果很好。可能与 Viko 提供的帖子有关。

      【讨论】:

        猜你喜欢
        • 2023-01-16
        • 2011-02-01
        • 2011-12-05
        • 1970-01-01
        • 2015-11-24
        • 2011-04-29
        • 2011-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多