WPF数据网格中字体的条件格式

本文关键字:条件 格式 字体 数据 数据网 网格 WPF | 更新日期: 2023-09-27 18:02:08

我有一个使用数据集填充的WPF数据网格。我正在尝试更改两列数据的字体颜色。我使用

REMOVED OLD CODE

但它并不能很好地保持颜色,尤其是当在网格上滚动时。它也很慢。

使用IValueCoverter有可能做到这一点吗?或者有其他更有效的方法来实现这一点?

编辑

我试图用一种新的方法来解决这个问题。我创建了一个类来返回一个bool,然后使用这个bool来确定字体是绿色还是红色

class EqualValuesColourConverter
{
    public static void ChangeColours(int pQty, int pPri, int pTot, int gQty, int gPri, int gTot)
    {
        int iqty = pQty;
        int gqty = gQty;
        int iprice = pPri;
        int gprice = gPri;
        int itotal = pTot;
        int gtotal = gTot;
        bool fontColor = true;
        if ((iqty == gqty) && (iprice == gprice) && (itotal == gtotal)) fontColor = true;
        else fontColor = false;
    }
 }

呼叫类别

 string iqty = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Quantity"].ToString();
            string gqty = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Quantity"].ToString();
            string iprice = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Price"].ToString();
            string gprice = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Price"].ToString();
            string itotal = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Total"].ToString();
            string gtotal = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Total"].ToString();
            int pQty = int.Parse(iqty);
            int pPri = int.Parse(iprice);
            int pTot = int.Parse(itotal);
            int gQty = int.Parse(gqty);
            int gPri = int.Parse(gprice);
            int gTot = int.Parse(gtotal);
            EqualValuesColourConverter.ChangeColours(pQty, pPri, pTot, gQty, gPri, gTot);

XAML

<DataGridTextColumn Width="61" Header="Inv_Quantity" Binding="{Binding Inv_Quantity}">
<DataGridTextColumn.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Foreground" Value="Green"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding EqualValuesColourConverter}" Value="False" >
                <Setter Property="Foreground" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGridTextColumn.CellStyle>


我试图让它在两列中工作,但它只会在一列中改变字体颜色。

有人能帮忙吗?

WPF数据网格中字体的条件格式

也许您可以尝试使用具有触发器在前景属性的样式来设置列单元格的样式?

http://blogs.msdn.com/b/jaimer/archive/2009/01/20/styling-microsoft-s-wpf-datagrid.aspx

int i = DgInvoiceLines.SelectedIndex;
            string iqty = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Quantity"].ToString();
            string gqty = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Quantity"].ToString();
            string iprice = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Price"].ToString();
            string gprice = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Price"].ToString();
            string itotal = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Total"].ToString();
            string gtotal = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Total"].ToString();
            DataGridCell InvQtyCell = GetCell(i, 2);
            DataGridCell GrnQtyCell = GetCell(i, 3);
            DataGridCell InvPriCell = GetCell(i, 4);
            DataGridCell GrnPriCell = GetCell(i, 5);
            DataGridCell InvTotCell = GetCell(i, 6);
            DataGridCell GrnTotCell = GetCell(i, 7);
            string InvoiceCellContentType = InvQtyCell.Content.GetType().Name.ToString();
            string GRNCellContentType = GrnQtyCell.Content.GetType().Name.ToString();
            string InvPriContentType = InvPriCell.Content.GetType().Name.ToString();
            string GrnPriContentType = GrnPriCell.Content.GetType().Name.ToString();
            string InvTotCellType = InvTotCell.Content.GetType().Name.ToString();
            string GrnTotCelType = GrnTotCell.Content.GetType().Name.ToString();
            if (iqty == gqty) 
            {
                if (InvoiceCellContentType == "TextBlock") ((TextBlock)InvQtyCell.Content).Foreground = Brushes.DarkGreen;
                else if (InvoiceCellContentType == "TextBox") ((TextBox)InvQtyCell.Content).Foreground = Brushes.DarkGreen;
                if (GRNCellContentType == "TextBlock") ((TextBlock)GrnQtyCell.Content).Foreground = Brushes.DarkGreen;
                else if (GRNCellContentType == "TextBox") ((TextBox)GrnQtyCell.Content).Foreground = Brushes.DarkGreen;
            }
            else
            {
                if (InvoiceCellContentType == "TextBlock") ((TextBlock)InvQtyCell.Content).Foreground = Brushes.Red;
                else if (InvoiceCellContentType == "TextBox") ((TextBox)InvQtyCell.Content).Foreground = Brushes.Red;
                if (GRNCellContentType == "TextBlock") ((TextBlock)GrnQtyCell.Content).Foreground = Brushes.Red;
                else if (GRNCellContentType == "TextBox") ((TextBox)GrnQtyCell.Content).Foreground = Brushes.Red;
            }
            if (iprice == gprice) 
            {
                if (InvPriContentType == "TextBlock") ((TextBlock)InvPriCell.Content).Foreground = Brushes.DarkGreen;
                else if (InvPriContentType == "TextBox") ((TextBox)InvPriCell.Content).Foreground = Brushes.DarkGreen;
                if (GrnPriContentType == "TextBlock") ((TextBlock)GrnPriCell.Content).Foreground = Brushes.DarkGreen;
                else if (GrnPriContentType == "TextBox") ((TextBox)GrnPriCell.Content).Foreground = Brushes.DarkGreen;
            }
            else
            {
                if (InvPriContentType == "TextBlock") ((TextBlock)InvPriCell.Content).Foreground = Brushes.Red;
                else if (InvPriContentType == "TextBox") ((TextBox)InvPriCell.Content).Foreground = Brushes.Red;
                if (GrnPriContentType == "TextBlock") ((TextBlock)GrnPriCell.Content).Foreground = Brushes.Red;
                else if (GrnPriContentType == "TextBox") ((TextBox)GrnPriCell.Content).Foreground = Brushes.Red;
            }

万一有人想要答案,只要在中调用,这实际上就有效

DgInvoiceLines_CellEditEnding仅DgInvoice Lines_CurrentCellChanged

我在DgInvoiceLines_SelectionChanged中调用了它,这似乎使它表现得很奇怪。

啊!