简体   繁体   中英

How to set Style property in Code Behind for Specific DataRow

I'm adding rows in code behind to my DataGrid defined in Xaml.

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?

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.

You can use DataTrigger to do this. In the example below if the State has value of State1 , it will be red and if it is State2 , it will be 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.

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;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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