C# WPF图像样式数据触发器绑定无法进行更改?
我有以下数据网格:C# WPF图像样式数据触发器绑定无法进行更改?,c#,wpf,user-interface,C#,Wpf,User Interface,我有以下数据网格: <DataGrid ItemsSource="{Binding PluginItems}" DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}" x:Name="dtg_Plugins" HorizontalAlignment="Left" VerticalAlignment="Top" Height="315" Width="446" CanUserAddRows="Fa
<DataGrid ItemsSource="{Binding PluginItems}" DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}" x:Name="dtg_Plugins" HorizontalAlignment="Left" VerticalAlignment="Top" Height="315" Width="446" CanUserAddRows="False" AutoGenerateColumns="False" SelectionMode="Single" Padding="0" RowHeight="26">
它包含以下列:
<DataGridTemplateColumn CanUserResize="False" CanUserReorder="False" Width="26">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="btn_PluginPlay" Padding="0" Height="26" Style="{StaticResource MetroFlatButton}" Background="Transparent" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Click="btn_PluginPlay_Click">
<Image Width="26" Height="26">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{StaticResource PlayIcon}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsRunning, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="False">
<Setter Property="Source" Value="{StaticResource PlayIcon}"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsRunning, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="True">
<Setter Property="Source" Value="{StaticResource StopIcon}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我不知道为什么,但如图所示更改图像的DataTrigger不起作用。
ItemsSource是可观察的集合,其余的工作正常
原因可能是什么?您只需要一个
数据触发器
:
<Button x:Name="btn_PluginPlay" Padding="0" Height="26" Style="{StaticResource MetroFlatButton}"
Background="Transparent" VerticalContentAlignment="Top" HorizontalContentAlignment="Left"
Click="btn_PluginPlay_Click">
<Image Width="26" Height="26">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{StaticResource PlayIcon}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsRunning}" Value="True">
<Setter Property="Source" Value="{StaticResource StopIcon}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Button>
然后,如果
PluginItem
类或您称之为的任何类具有名为IsRunning
的公共bool
属性,该属性在设置为新值时会发出更改通知,那么它应该可以工作。请注意,在DataTrigger绑定上设置Mode=TwoWay
和UpdateSourceTrigger=PropertyChanged
是毫无意义的。它没有效果。也就是说,这里有一个标准的愚蠢问题:您已经实现了INotifyPropertyChanged,并且在IsRunning属性更改时正在触发PropertyChanged事件?谢谢!我已经实现了yep,这就是为什么它毫无意义:(当IsRunning为真时,它显示PlayIcon,但不显示StopIcon?MetroFlatButton样式不会以某种方式弯曲DataContext属性?IsRunning是一个公共属性?为什么我要问所有这些?请显示代码的所有相关部分。