2011-07-14 42 views
1

我有一個使用數據集填充的WPF Datagrid。我試圖改變兩列數據的字體顏色。我有它使用WPF Datagrid中字體的條件格式

REMOVED OLD CODE 

但它不是很好地保持顏色,特別是當在網格上滾動。它也很慢。

是否有可能使用IValueCoverter來做到這一點,或者是否有其他更有效的方法來實現這一點?

編輯

我曾試圖採取一種新的方法解決這個問題。我創建了一個類來返回一個布爾值,然後使用該布爾值來確定字體是綠色還是紅色。

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類

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> 


我試圖讓它在兩列工作,但它只會改變字體顏色之一。

任何人都可以幫忙嗎?

回答

0
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只有DgInvoiceLines_CurrentCellChanged

我已經在DgInvoiceLines_SelectionChanged中調用它,這似乎使它表現得很奇怪。

HTH!