单击时更改datagrid单元格中按钮的图像 - wpf

时间:2013-09-19 09:13:36

标签: c# 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中,我需要编写一个代码来在单击按钮时切换图像(b / w TickImageCrossImage)。换句话说,我需要一种方法来同时将Button与ICommandBitmapImage变量绑定。

请帮助!!

1 个答案:

答案 0 :(得分:4)

如果你的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>
相关问题