简体   繁体   中英

WPF Datagrid DataBinding DataView

Pre-Warning sorry WPF new guy here:

I have a DataGrid bound to a DataTable's DefaultView

ResultDataGrid.ItemsSource = resultTable.DefaultView;    

I know the column names, and I need to change a column's foreground if another column is 1 (always 0 or 1)

Currently what I have:

private void ResultDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
        if (e.Column.Header.ToString() == "columnName")
        {
            e.Column.CellStyle = FindResource("columnStyle") as Style;
        }
}

and in XAML:

<Window.Resources>
    <Style TargetType="DataGridCell" x:Key="columnStyle">
        <Setter Property="Foreground" Value="Black"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding resultTable, Path={StaticResource otherColumnName}}" Value="1">
                <Setter Property="Foreground" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

Where otherColumnName is set in the constructor

public ResultsCustom(DataTable resultclass, CustomQuery query)
{
    // Some other stuff
    this.Resources.Add("otherColumnName", COLUMN_NAME);
}

The XAML Style seems to not have the correct path, any help would be appreciated!

I'm not sure about this line of code:

Path={StaticResource otherColumnName}

Are you attempting to compare the property "otherColumnName" of the resultTable to see if this is 1? This wouldn't be a static resource, and you could change your binding to:

Path=otherColumnName

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