【问题标题】:WPF - Button control template...keep template items enabled when Command.CanExecute is falseWPF - 按钮控件模板...当 Command.CanExecute 为 false 时保持模板项启用
【发布时间】:2012-12-30 01:39:08
【问题描述】:

我有一个 listView,其中每个项目都包含一个包含 Button 的 dataTemplate。这是由于要求单击列表项将像单击按钮一样使您进入“向导”的下一步。

DataTemplate 中的每个 Button 都包含一个 ControlTemplate。

当命令的 CanExecute 为 false 时,此 controlTemplate 内的项目将被禁用。特别是在我下面的示例中,Button 模板(以图像作为模板的那个)内的 Button 也被禁用。

即使与该按钮关联的命令被禁用,我如何才能使模板内的项目保持启用状态。

总结:按钮的模板包含另一个按钮。当父按钮的命令被禁用时,模板内的按钮被禁用。

XAML:

<ListView.ItemTemplate>
    <DataTemplate>                
        <Button Name="nextButton" Height="30" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Wiz:edited}}, Path=DataContext.ACommand}" CommandParameter="{Binding}">
              <Button.Template>
                <ControlTemplate>                     
                   <StackPanel Orientation="Horizontal">
                      <Label Content="{Binding}"/>
                      <Button>
                         <Button.Template>
                            <ControlTemplate>
                                <Image Source="{Binding source}"/>
                            </ControlTemplate>
                         </Button.Template>
                      </Button>
                   </StackPanel>
                </ControlTemplate>                    
              </Button.Template>
            </Button>
    </DataTemplate>
</ListView.ItemTemplate>

【问题讨论】:

    标签: wpf command datatemplate controltemplate canexecute


    【解决方案1】:

    ButtonBase 挂钩与其关联的 CommandCanExecute。当 CanExecute 返回 false 时,ButtonBase 缓存它并从其 IsEnabledCore 返回 false,从而禁用 ButtonBase。当一个元素被禁用时,它的后代也被禁用。如果您不希望它被禁用,那么您不应该将它与命令相关联(例如挂钩单击)或不使用 CanExecute 将为假的命令(例如使用从CanExecute 但不调用包装命令的执行)或不将其他元素放在按钮内(创建/使用一些附加行为,当单击列表项时将执行命令)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多