繁体   English   中英

单击DataGrid单元格中的按钮时更改其图像-WPF

[英]Change image of a button in a datagrid cell when it is clicked - wpf

我有一个WPF应用程序,其中包含由一列按钮组成的Datagrid。 相应的XAML代码如下。

<DataGridTemplateColumn Header="Closed" Width="60">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button>
                <Button.Content>
                    <Image>
                        <Image.Source>
                            <MultiBinding Converter="{StaticResource ImageConverter}"
                                  ConverterParameter="Closed">
                                <Binding Path="IsClosed"/>
                                <Binding Path="DataContext.TickImage" RelativeSource="{RelativeSource AncestorType={x:Type UserControl}}"/>
                                <Binding Path="DataContext.CrossImage" RelativeSource="{RelativeSource AncestorType={x:Type UserControl}}"/>
                            </MultiBinding>
                        </Image.Source>
                    </Image>
                </Button.Content>
                <Button.Command>
                    <Binding Path="DataContext.ClosedClicked" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}"/>
                </Button.Command>
            </Button>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

TickImageCrossImage是图像源,可以根据IsClosed的值进行选择。 现在,在ViewModel中,我需要编写代码来在单击按钮时切换图像(黑白TickImageCrossImage )。 换句话说,我需要一种同时将Button与ICommandBitmapImage变量绑定的方法。

请帮忙!!

如果您的IsClosed只是一个开关,可以知道是否按下了按钮或释放了按钮,则可以通过使用以下触发器来实现:

 <ToggleButton>
     <ToggleButton.Content>
         <Image>
            <Image.Style>
              <Style TargerType="Image">
                <Setter Property="Source" Value="{Binding Path="DataContext.TickImage" RelativeSource="{RelativeSource AncestorType={x:Type UserControl}}"/>
                 <Style.Triggers>
                      <DataTrigger Binding="{Binding IsChecked, RelativeSource="{RelativeSource AncestorType={x:Type ToggleButton}}" Value="true">
                         <Setter Property="Source" Value="{Binding Path="DataContext.CrossImage" RelativeSource="{RelativeSource AncestorType={x:Type UserControl}}"/>
                      </DataTrigger>
                  </Style.Triggers>
                </Style>
             </Image.Style>
            </Image>
          </ToggleButton.Content> 
        </ToggleButton>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM