WPF数据网格问题

时间:2010-05-17 15:41:15

标签: c# datagrid

是否可以设置一行或多行的背景颜色?

1 个答案:

答案 0 :(得分:0)

您可以使用AlternatingRowBackground属性执行每隔一行。你需要做特定的行吗?只像第1,5和9行?或者不同的背景颜色取决于数据?

已编辑:根据评论进行编辑..

可以像这样做基于数据...

<DataGrid 
    AlternatingRowBackground="AliceBlue" 
    ItemsSource="{Binding}" 
    AutoGenerateColumns="False" 
    Height="200" 
    HorizontalAlignment="Left"
    Margin="156,58,0,0" 
    Name="dataGrid1" 
    VerticalAlignment="Top"
    Width="200" LoadingRow="dataGrid1_LoadingRow">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding}" />
    </DataGrid.Columns>
</DataGrid>

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    ObservableCollection<string> list = new ObservableCollection<string>();
    list.Add("test1");
    list.Add("test2");
    list.Add("test3");
    list.Add("test4");
    list.Add("test5");

    dataGrid1.ItemsSource = list;
}

private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
    if (e.Row.Item.ToString().Contains("test"))
    {
        e.Row.Background = Brushes.Red;
    }
}