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

谁说我不能喝 提交于 2020-01-11 14:07:14

问题


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?


回答1:


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>



回答2:


You could use the Click eventhandler of the Button. Then you don't need any Commands.

private void bt_Delete_Click(object sender, RoutedEventArgs e)
{
    var btn = sender as Button;
    YourClass dc = btn.DataContext as YourClass;
    dc.IsToDelete = true;
}

I don't really like this solution, but I think, it would work.



来源:https://stackoverflow.com/questions/37564612/wpf-how-to-set-bool-to-true-from-datatemplate-for-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!