我目前正在开发一个WPF应用程序,它包含一个包含3列Closed
,Checked
和Active
的数据网格,如XAML代码所示,它们链接到3个布尔DataObject成员(IsClosed
,IsCheck
和IsActive
)。在单击每个togglebuttons时,绑定的布尔属性必须更改,并且基于布尔数据,togglebuttons的图像必须更改。
单击相应的togglebuttons后,后端代码中的数据会发生变化,但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时图像会发生变化(尽管绑定属性会受到影响)。
答案 0 :(得分:1)
更新您的DataTrigger绑定到Togglebutton的属性,如IsChecked,IsEnabled等,而不是上下文属性:
<DataTrigger Binding="{Binding Path=IsChecked, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="true">
<Setter Property="Source" Value="{Binding Path=IsClosed, Converter={StaticResource imageConverter}}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsChecked, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="false">
<Setter Property="Source" Value="{Binding Path=IsClosed, Converter={StaticResource imageConverter}}"/>
</DataTrigger>
另外我不明白为什么你需要触发器...你没有改变触发器中的任何东西...你在触发器设置器中所做的绑定与默认设置器相同...所以它应该在没有触发器的情况下工作