我花了很多时间尝试在 CheckBox 单击事件上更改行文本颜色。
因此,如果选中复选框,我们需要将颜色设置为灰色,如果未选中复选框,则将其恢复为正常颜色。
请帮助获得此结果(DataGrid 绑定到 xml 文件)。
更新。一些代码:
XAML 用于基于选中列(复选框)更改行颜色:
<Window.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Foreground" Value="{Binding Checked, Converter={x:Static local:MainWindow.rowCheckedConverter}}" />
</Style>
</Window.Resources>
...
<Grid.DataContext>
<XmlDataProvider x:Name="userTasksProvider" XPath="UserTasks" />
</Grid.DataContext>
...
<DataGrid Name="dgUserTasks" Grid.Column="1" Margin="1,0,0,0"
AutoGenerateColumns="False" HeadersVisibility="None"
ItemsSource="{Binding XPath=Task}">
<DataGrid.Columns>
<DataGridCheckBoxColumn x:Name="cbUserTasksColumn" Width="20"
Binding="{Binding Checked,
Mode=TwoWay}" Header="">
</DataGridCheckBoxColumn>
<DataGridTextColumn
x:Name="Info" Width="*"
Binding="{Binding Info,
Mode=TwoWay}"
Header="" >
</DataGridTextColumn>
...
C#WPF:
public partial class MainWindow : Window
{
public static RowCheckedConverter rowCheckedConverter = new RowCheckedConverter();
...
}
...
public class RowCheckedConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
if ((bool)value) {
return new SolidColorBrush(Colors.Gray);
}
else {
return new SolidColorBrush(Colors.Black);
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
throw new Exception("The method or operation is not implemented.");
}
}