WPF Datagrid中字体的条件格式化 [英] Conditional formatting of the font in a WPF Datagrid

查看:126
本文介绍了WPF Datagrid中字体的条件格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用数据集填充的WPF Datagrid。我试图将数据的字体颜色更改为两列。我正在使用

 删除旧代码

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



是否可以使用 IValueCoverter 或者还有其他更有效的方式实现这一点?



编辑



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



CLASS

  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 =61Header =Inv_QuantityBinding ={Binding Inv_Quantity}> 
< DataGridTextColumn.CellStyle>
< Style TargetType ={x:Type DataGridCell}>
< Setter Property =ForegroundValue =Green/>
< Style.Triggers>
< DataTrigger Binding ={Binding EqualValuesColourConverter}Value =False>
< Setter Property =ForegroundValue =Red/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /DataGridTextColumn.CellStyle>
















我试图得到它可以在两列中工作,但只能将字体颜色更改为一个。



任何人都可以帮助?

解决方案

  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 =画笔。深绿色;
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 DgInvoiceLines_CurrentCellChanged 中调用。



我已经在 DgInvoiceLines_SelectionChanged 中调用它,这似乎使其行为奇怪。



HTH!


I have a WPF Datagrid populated using a Dataset. I am trying to change the font colour of the data in two columns. I have it working using

REMOVED OLD CODE

But it isn't holding the colours very well especially when there is scrolling on the grid. It is also slow.

Is it possible to do this using an IValueCoverter or is there some other more efficient way to achieve this?

EDIT

I have tried to take a new approach to this problem. I have created a class to return a bool and then use that bool to determine whether the font will be green or red.

CLASS

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;

    }
 }

CALL TO CLASS

 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>


I am trying to get it to work in two columns but it will only change the font colour in one.

Can anyone help?

解决方案

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

Just in case any one wanted the answer this actually works as long as it is called in

DgInvoiceLines_CellEditEnding and DgInvoiceLines_CurrentCellChanged only.

I had called it in DgInvoiceLines_SelectionChanged which seems to make it behave strangely.

HTH!

这篇关于WPF Datagrid中字体的条件格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆