简体   繁体   English

如何在特定于DataRow的代码隐藏中设置样式属性

[英]How to set Style property in Code Behind for Specific DataRow

I'm adding rows in code behind to my DataGrid defined in Xaml. 我将代码行添加到Xaml中定义的DataGrid中。

DataTable dt = new DataTable();
DataRow dr1 = dt.NewRow();
DataRow dr2 = dt.NewRow();
DataRow dr3 = dt.NewRow();

dr1.Style = (Style)Resources.FindName("CustomStyle"); 

dataGrid.ItemSource = dt;

Well dr1 doesn't have a property called Style so how can I do this? 那么dr1没有一个名为Style的属性,那么我该怎么做呢?

This should be pretty simple but I'm having a hard time. 这应该很简单,但是我很难过。

I know this is really simple to do in XAML but needs to be done in code behind since I can't define the amount of rows in XAML since they'll be dynamically added. 我知道这在XAML中确实很简单,但是需要在后面的代码中完成,因为我无法在XAML中定义行数,因为它们会动态添加。

You can use DataTrigger to do this. 您可以使用DataTrigger来执行此操作。 In the example below if the State has value of State1 , it will be red and if it is State2 , it will be Green . 在下面的示例中,如果State值为State1 ,它将为红色;如果值为State2 ,则将为Green You can bind it to another property of your datatable, another value and whatever color you prefer. 您可以将其绑定到数据表的另一个属性,另一个值以及您喜欢的任何颜色。

<DataGrid ItemsSource="{Binding YourItemsSource}">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow"> 
            <Style.Triggers>
                <DataTrigger Binding="{Binding State}" Value="State1">
                    <Setter Property="Background" Value="Red"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding State}" Value="State2">
                    <Setter Property="Background" Value="Green"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

It can be done in Xaml as answered by CodingYoshi. CodingYoshi回答说,可以在Xaml中完成。

If you need complete codebehind solution then you can refer this. 如果您需要完整的代码隐藏解决方案,则可以参考此。

   public MainWindow()
    {
        InitializeComponent();


        DataTable dt = new DataTable();
        dt.Columns.Add("Col", typeof(string));

        DataRow dr1 = dt.NewRow();
        dr1[0] =  "row1" ;
        DataRow dr2 = dt.NewRow();
        dr2[0] = "row2";
        DataRow dr3 = dt.NewRow();
        dr3[0] = "row3";

        dt.Rows.Add(dr1);
        dt.Rows.Add(dr2);
        dt.Rows.Add(dr3);

        dataGrid.ItemsSource = dt.AsDataView();

        dataGrid.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
    }

    private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
    {
        // This will ensure, items are generated over UI.
        if (dataGrid.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
        {
            int index = 1; // add logic to get index of row to be styled.
            var row = (DataGridRow)dataGrid.ItemContainerGenerator
                                                 .ContainerFromIndex(index);

            // creating style, can be picked from resources aswell.
            Style style = new Style
            {
                TargetType = typeof(Control)
            };

            style.Setters.Add(new Setter(Control.BackgroundProperty, Brushes.Green));
            // Applied logic
            row.Style = style;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM