如何更改复选框上的DataGrid行颜色?

时间:2012-07-10 06:32:16

标签: c# wpf datagrid

我花了很多时间尝试在CheckBox点击事件上更改行文字颜色。

因此,如果选中复选框,我们需要将颜色设置为灰色,如果未选中复选框,则将其恢复为正常颜色。

请帮助获得此结果(DataGrid绑定到xml文件)。

UPD。一些代码:

根据列Checked(复选框)更改行颜色的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.");
        }
    }       

2 个答案:

答案 0 :(得分:0)

你的问题很模糊,但我会尽力帮助。

考虑这个例子:

 <DataGrid ItemsSource="{Binding DataSet}" AutoGenerateColumns="False">
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsActive}" Value="False">
                        <Setter Property="Foreground" Value="Gray"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}">
            </DataGridTextColumn>
            <DataGridCheckBoxColumn Binding="{Binding IsActive, Mode=TwoWay}">

            </DataGridCheckBoxColumn>

        </DataGrid.Columns>
    </DataGrid>

和代码:

public partial class MainWindow : Window
{
    public ObservableCollection<Data> DataSet { get; set; }

    public MainWindow()
    {
        DataSet = new ObservableCollection<Data>();
        DataSet.Add(new Data { Name = "First" });
        DataSet.Add(new Data { Name = "Second" });
        DataSet.Add(new Data { Name = "Third" });
        InitializeComponent();
        DataContext = this;
    }

}

public class Data
{
    public string Name { get; set; }

    public bool IsActive { get; set; }
}

在此片段中,我将复选框列绑定到Data实例中的属性,并在此属性为false时添加样式触发器。这是你在哪里寻找的?

问题更新后修改

我无法在代码中看到您提供的样式分配给DataGrid的位置:

<DataGrid  RowStyle="{StaticResource myStyle}" ...>

并且您的样式未声明x:Key属性。

如果你添加它,它会工作,但静态fied /属性只是闻起来很糟糕。考虑改为:

<Style TargetType="DataGridRow" x:Key="myStyle">
    <Style.Triggers>
          <DataTrigger Binding="{Binding Checked}" Value="False">
               <Setter Property="Foreground" Value="Gray"/>
          </DataTrigger>
    </Style.Triggers>
</Style>

如果您决定使用转换器,请考虑改变:

<Style TargetType="{x:Type DataGridRow}" x:Key="myStyle">
    <Setter Property="Foreground" >
        <Setter.Value>
            <Binding Path="Checked">
                <Binding.Converter>
                    <local:RowCheckedConverter />
                </Binding.Converter>
            </Binding>
        </Setter.Value>
    </Setter>
</Style>

这也将消除此字段。

答案 1 :(得分:0)