I have a listView where each item consists of a dataTemplate containing a Button. This is due to a requirement where clicking on the list item will act like a button click to move you into the next step of a "Wizard".

Each Button in the DataTemplate contains a ControlTemplate.

The items inside of this controlTemplate are becoming disabled when the command's CanExecute is false. Specifically in my example below, the Button inside of the Button's template (the one with an image as it's template) is becoming disabled as well.

How can I keep the items inside of the template enabled even when the command associated with that button is disabled.

Summary: Button has template that contains another button. Button inside of template is disabled when parent button's command is disabled.

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>
有帮助吗?

解决方案

ButtonBase hooks the CanExecute of the Command it is associated with. When the CanExecute returns false the ButtonBase caches that and returns false from its IsEnabledCore thereby disabling the ButtonBase. When an element is disabled, its descendants are disabled. If you don't want it to be disabled then you either shouldn't associate it with a command (e.g. hook the click) or don't use a command whose CanExecute will be false (e.g. use a different wrapper command that returns true from CanExecute but doesn't call the Execute of the wrapped command) or don't put other elements inside the button (create/use some attached behavior that will execute a command when the listitem is clicked).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top