简体   繁体   English

C#WPF DataGrid基于值更改行的字体颜色

[英]C# WPF DataGrid Change FontColour of Row Based on Value

I'm retrieving data from a Database and placing the contents into a WPF DataGrid like so; 我正在从数据库中检索数据,然后将内容放入WPF DataGrid中;

namespace ContractsExcel { public partial class UserSelection : Page { public UserSelection() { InitializeComponent(); 命名空间ContractsExcel {公共部分类UserSelection:页面{public UserSelection(){InitializeComponent(); dataGrid.CanUserAddRows = false; dataGrid.CanUserAddRows = false; string username = Environment.UserName; 字符串用户名= Environment.UserName; userImage.Source = new BitmapImage(new Uri(@"C:\\Users\\DanD\\Desktop\\" + username+".jpg")); userImage.Source =新的BitmapImage(新的Uri(@“ C:\\ Users \\ DanD \\ Desktop \\” +用户名+“。jpg”)); } }

    private void FillDataGrid(object sender, RoutedEventArgs e)
    {
        string constr = ConfigurationManager.ConnectionStrings["dbfString"].ConnectionString;
        using (OleDbConnection dbfCon = new OleDbConnection(constr))
        {
            try
            {
                dbfCon.Open();
                DataTable dTable = new DataTable();
                string dbfQuery = "SELECT em_pplid, em_name, em_netname FROM employs WHERE em_netname NOT LIKE ''";
                OleDbCommand MyQuery = new OleDbCommand(dbfQuery, dbfCon);
                OleDbDataAdapter DA = new OleDbDataAdapter(MyQuery);
                DA.Fill(dTable);
                dataGrid.ItemsSource = dTable.AsDataView();
            }
            catch (OleDbException)
            {
                throw;
            }
        }
    }
}

} }

What I would like to do is change the font colour of an entire row based on the contents of a row cell, for example if the order is cherished change the font colour of the row to red. 我想做的是根据行单元格的内容更改整个行的字体颜色,例如,如果珍惜顺序,请将行的字体颜色更改为红色。

Whilst there are multiple questions about this topic existing already I couldn't find anything specific to this. 尽管已经存在有关此主题的多个问题,但我找不到与此相关的任何特定内容。 Would this need to be done through XAML or C# and how woild I go about implementing this functionality? 是否需要通过XAML或C#完成此操作,以及如何实现此功能?

Updated code with XAML; 使用XAML更新代码;

    <DataGrid x:Name="dataGrid" Margin="0,0,10,0" Grid.ColumnSpan="3" ColumnWidth="*" FontSize="18.667">
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding em_netname}" Value='Chris'>
                        <Setter Property="Foreground" Value="Red"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>

Code to populate DataContext in C#; 在C#中填充DataContext的代码;

namespace ContractsExcel { public partial class UserSelection : Page { public UserSelection() { InitializeComponent(); 命名空间ContractsExcel {公共部分类UserSelection:页面{public UserSelection(){InitializeComponent(); dataGrid.CanUserAddRows = false; dataGrid.CanUserAddRows = false; string username = Environment.UserName; 字符串用户名= Environment.UserName; userImage.Source = new BitmapImage(new Uri(@"C:\\Users\\DanD\\Desktop\\" + username+".jpg")); userImage.Source =新的BitmapImage(新的Uri(@“ C:\\ Users \\ DanD \\ Desktop \\” +用户名+“。jpg”)); } }

    private void FillDataGrid(object sender, RoutedEventArgs e)
    {
        string constr = ConfigurationManager.ConnectionStrings["dbfString"].ConnectionString;
        using (OleDbConnection dbfCon = new OleDbConnection(constr))
        {
            try
            {
                dbfCon.Open();
                DataTable dTable = new DataTable();
                string dbfQuery = "SELECT em_pplid, em_name, em_netname FROM employs WHERE em_netname NOT LIKE ''";
                OleDbCommand MyQuery = new OleDbCommand(dbfQuery, dbfCon);
                OleDbDataAdapter DA = new OleDbDataAdapter(MyQuery);
                DA.Fill(dTable);
                dataGrid.ItemsSource = dTable.AsDataView();
            }
            catch (OleDbException)
            {
                throw;
            }
        }
    }
}

} }

  1. You need DataTriggers for such scenarios. 对于这种情况,您需要DataTriggers。
  2. Changing Foreground of a DataGridCell will change that of entire row as style is applied to all cells. 当样式应用于所有单元格时,更改DataGridCell的前景将更改整个行的前景。

Below style will make cherished order rows appear to be with Red font color and rest rows will have font color as Aqua. 下面的样式将使珍贵的订单行显示为红色字体颜色,其余行的字体颜色为Aqua。

    <Style TargetType="DataGridCell">
    <Setter Property="Foreground" Value="Aqua"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Order}" Value="Cherished">
            <Setter Property="Foreground" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
    </Style>

Another approach is to use 另一种方法是使用

           <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="Foreground" Value="Aqua"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Title}" Value="Pepsi">
                            <Setter Property="Foreground" Value="Red"/>
                        </DataTrigger>
                    </Style.Triggers>

                </Style>
            </DataGrid.RowStyle>

write data template for data grid with custom colour for rows. 使用自定义颜色为行编写数据网格的数据模板。 you are assigning data table directly to grid. 您是将数据表直接分配给网格。 so that you cannot change colour based on data. 这样就无法根据数据更改颜色。 else you have to write model class for colour then only can set colour based on data. 否则,您必须为颜色编写模型类,然后只能根据数据设置颜色。

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

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