WPF DataGrid AlternatingRowBackground and RowStyle precedence

寵の児 提交于 2019-12-10 02:47:24

问题


How do I make my RowStyle to get applied after AlternatingRowBackground? I want items, having IsOrange as true to have Orange background regardless of alternating row background, which isn't the case currently.

XAML:

<DataGrid Name="g"
    AlternatingRowBackground="Blue" 
    AlternationCount="2" 
    ...
    SelectionMode="Single">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsOrange}" Value="Y">
                    <Setter Property="Background" Value="Orange" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
    ...
</DataGrid>

回答1:


It's not a bug. In a Style you can't override a local value set for the alternating row. That's why this will not work

<DataGrid AlternatingRowBackground="Blue"

But if you set AlternatingRowBackground in a Style you can

<DataGrid.Style>
    <Style TargetType="DataGrid">
        <Setter Property="AlternatingRowBackground" Value="Blue"/>
    </Style>
</DataGrid.Style>

Thanks to this answer.




回答2:


In my program I have two classes in addition to main window that contains one DataGird only. Let's start with first class:

MyClass.cs:

public class MyClass
{
    public bool IsOrange { get; set; }

    public string Name { get; set; }
}

I have only two properties, IsOrange specifies whether the row should be orange. ((Do not care for the other property.))

Now the view model class contains only collection of MyClass.

MyClassViewModel.cs:

public class MyClassViewModel
{
    public ObservableCollection<MyClass> con { get; set; }

    public MyClassViewModel()
    {
        con = new ObservableCollection<MyClass>();

        con.Add(new MyClass { IsOrange = true, Name = "Aa" });
        con.Add(new MyClass { IsOrange = true, Name = "Bb" });
        con.Add(new MyClass { IsOrange = false, Name = "Cc" });
        con.Add(new MyClass { IsOrange = false, Name = "Dd" });
        con.Add(new MyClass { IsOrange = false, Name = "Ee" });
        con.Add(new MyClass { IsOrange = true, Name = "Ff" });
        con.Add(new MyClass { IsOrange = true, Name = "Gg" });
        con.Add(new MyClass { IsOrange = false, Name = "Hh" });
    }
}

In MainWindow.xaml:

<Grid>
    <DataGrid Margin="10" ItemsSource="{Binding Path=con}" >
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsOrange}" Value="true">
                        <Setter Property="Background" Value="Orange" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>
</Grid>

finally in MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    MyClassViewModel VM = new MyClassViewModel();

    public MainWindow()
    {
        InitializeComponent();

        DataContext = VM;
    }
}

and this is the result:

you can send me your email to send you the app.

Good Luck :)



来源:https://stackoverflow.com/questions/13914382/wpf-datagrid-alternatingrowbackground-and-rowstyle-precedence

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!