Search code examples
wpfxamldata-bindingdatatemplate

WPF: How to set bool to true from DataTemplate for Button?


I know it is possible to do this by working with ICommand, but since this is the only place in my whole project where this is needed, I am asking myself, if there is maybe a better solution than implementing RelayCommand and ICommand and an additional method in my otherwise property-only class?

Maybe my scenario may help here: I have a ListView which is bound to a list of properties (this is a custom-class I made to display those). I have a button in eacht row of entries, that I want to set the "IsToDelete"-Property to true.

If "IsToDelete" is true, all the controls I show in my ListView will collapse.

The whole logic is up to this point in DataTemplates, the one for my button looks like this:

<DataTemplate x:Key="DeleteButtonTemplate">
  <Button Name="bt_Delete" 
   Command="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=ItemSource.DeleteClicked}">
    <Image Height="16" Width="16" Source="Images\RecycleBin\VSO_RecycleBin_16x.png"/>
    <Button.Style>
      <Style TargetType="{x:Type Button}">
        <Style.Triggers>
          <DataTrigger Binding="{Binding IsToDelete}" Value="True">
            <Setter Property="Visibility" Value="Collapsed"/>
          </DataTrigger>
        </Style.Triggers>
      </Style>
    </Button.Style>
  </Button>
</DataTemplate>

Note the command in this DataTemplate is currently not working as intended, that is why I even got to the question ;)

In the optimal case I'd just have to do something like:

<Setter Property="{Binding IsToDelete}" Value="True"/>

So, is there a way to solve it like this or at least in a less complicated way than ICommand with RelayCommand?


Solution

  • why not use ToggleButton and bind IsChecked property to IsToDelete property of a viewModel?

    simplified example with ItemsControl

    <ItemsControl>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <ToggleButton Content="X" IsChecked="{Binding Path=IsToDelete}"/>
                    <Border Width="100"                                
                            Background="MediumPurple" 
                            Margin="5">
    
                        <Border.Style>
                            <Style TargetType="Border">
                                <Setter Property="Visibility" Value="Visible"/>
                                <Style.Triggers>                                        
                                    <DataTrigger Binding="{Binding Path=IsToDelete}" Value="True">
                                        <Setter Property="Visibility" Value="Collapsed"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Border.Style>
    
                    </Border>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>            
    </ItemsControl>