我目前正在开发一个 WPF 应用程序,该应用程序包含一个具有 3 列的数据网格,Closed
如XAML 代码所示,它们分别链接到 3 个布尔数据对象成员(和)。单击每个切换按钮时,绑定的布尔属性必须更改,并且基于布尔数据,切换按钮的图像必须更改。单击相应的切换按钮后,后端代码中的数据正在更改,但无法正常工作。Checked
Active
IsClosed
IsCheck
IsActive
DataTrigger
XAML 代码:
<DataGrid.Columns>
...
<DataGridTemplateColumn Header="Closed" Width="60">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ToggleButton IsChecked="{Binding Path=IsClosed, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding Path=IsCheck, Converter={StaticResource toggleButtonEnableConverter}}">
<ToggleButton.Content>
<Image>
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="{Binding Path=IsClosed, Converter={StaticResource imageConverter}}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsClosed, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="true">
<Setter Property="Source" Value="{Binding Path=IsClosed, Converter={StaticResource imageConverter}}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsClosed, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="false">
<Setter Property="Source" Value="{Binding Path=IsClosed, Converter={StaticResource imageConverter}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</ToggleButton.Content>
</ToggleButton>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Checked" Width="60">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ToggleButton Visibility="{Binding Path=IsClosed,
Converter={StaticResource ResourceKey=booleanToVisibilityConverter}}"
IsChecked="{Binding Path=IsCheck, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding Path=IsActive, Converter={StaticResource toggleButtonEnableConverter}}">
<ToggleButton.Content>
<Image>
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="{Binding Path=IsCheck, Converter={StaticResource imageConverter}}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsCheck, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="true">
<Setter Property="Source" Value="{Binding Path=IsCheck, Converter={StaticResource imageConverter}}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsCheck, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="false">
<Setter Property="Source" Value="{Binding Path=IsCheck, Converter={StaticResource imageConverter}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</ToggleButton.Content>
</ToggleButton>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Active" Width="60">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ToggleButton Visibility="{Binding Path=IsCheck,
Converter={StaticResource ResourceKey=booleanToVisibilityConverter}}"
IsChecked="{Binding Path=IsActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ToggleButton.Content>
<Image>
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="{Binding Path=IsActive, Converter={StaticResource imageConverter}}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsActive, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="true">
<Setter Property="Source" Value="{Binding Path=IsActive, Converter={StaticResource imageConverter}}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsActive, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="false">
<Setter Property="Source" Value="{Binding Path=IsActive, Converter={StaticResource imageConverter}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</ToggleButton.Content>
</ToggleButton>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
转换器:
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (value is bool && ((bool)value)) ? Images.TickImage : Images.CrossImage;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class Images
{
public static BitmapImage TickImage = new BitmapImage(new Uri("K:\\projects\\ContentSets\\Ownership\\SOMA\\Staging\\Utility\\Images\\icon_tick.gif", UriKind.Absolute));
public static BitmapImage CrossImage = new BitmapImage(new Uri("K:\\projects\\ContentSets\\Ownership\\SOMA\\Staging\\Utility\\Images\\icon_cross.gif", UriKind.Absolute));
}
public class ToggleButtonEnableConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
图像在启动时根据布尔值加载(因此图像源没有问题),但图像在单击 ToggleButton 时会发生变化(尽管绑定的属性会受到影响)。