2013-03-27 145 views
6

すべて私はWPF DataGridをループして、各ループごとにaを使用して、誤ったセルの背景色を変更しようとしています。私は多くの質問をチェックしたが、まだ十分な答えを見つけていない。私はこれまで持っていることは問題WPF DataGridを使用したループ処理foreachを使用した

public void RunChecks() 
{ 
    const int baseColumnCount = 3; 
    foreach (DataRowView rv in dataGrid.Items) 
    { 
     for (int i = baseColumnCount; i < dataGrid.Columns.Count; i++) 
     { 
      if (!CheckForBalancedParentheses(rv.Row[i].ToString())) 
      { 
       Color color = (Color)ColorConverter.ConvertFromString("#FF0000"); 
       row.Background = new SolidColorBrush(color); // Problem! 
      } 
     } 
    } 
} 

である私のDataGridの行のBackground色を変更するために、私はDataRowViewrvでascociated DataGridRowオブジェクトを使用する必要があるということです。

オブジェクトrvDataRowView)からDataGridRowへの参照を取得するにはどうすればよいですか?

お時間をいただきありがとうございます。

編集。以下のアドバイスに基づいて、マウスオーバーイベントで動作し、関連するセルの前後のフォントを設定する次のスタイルを持つようになりました。しかし、私は本当に上のコードでランタイムにセルにバックカラーを適用する方法については失われています。あなたがWPF、常に直接UI成果物へのアクセス回避で作業しているときにXMLスタイルが

<Window.Resources> 
    <Style TargetType="{x:Type DataGridRow}"> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" 
        Value="True"> 
       <Setter Property="Background" Value="Red" /> 
       <Setter Property="FontWeight" Value="ExtraBold" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 

答えて

5

です。

ModelViewでプロパティColorを作成し、それをDataGridビューの単一行テンプレートの背景色にバインドします。

色を変更するには、ModelViewのcollectonをスローし、すべての単一の行にバインドされたすべての単一オブジェクトのColorプロパティを設定/読み込みます。これを変更することにより、バインディングが正しく設定されていれば、行UIの色の外観に影響します。あなたが上で見ることができ、具体的なサンプルについて

:あなたはあなたのデータの視覚的な表現を取得するためにItemContainerGeneratorを使用することができます

How do I bind the background of a data grid row to specific color?

+0

お時間をいただきありがとうございます。私はあなたの助言を取り、読書を始めます。私はまた色とりどりの列に、また運がないように努力してきました。私は別の質問[ここ](http://stackoverflow.com/questions/15644105/change-the-background-color-of-entire-column-of-wpf-datagrid-at-runtime)を借りることができたら担当者を欲しい。もう一度ありがとう... – MoonKnight

+0

@Killercam:考え方は行と列で同じです。 UIオブジェクトが、直接アクセスまたはバインディングを介して変更できるプロパティ(この場合はcolor)を提供している場合は、modelviewオブジェクトからバインディングを使用します。 – Tigran

+0

ありがとうございます。 Styles、Triggersなどの本を読んだことがありました。私は今、イベントでマウスの色を変える行を持っています。しかし、私はどのようにこれらのスタイルを使用して実行時にコードから必要な色にセル/行を変更するために失われています。あなたがここで何か助けを与えることができれば、私はそのスタイルを示すために質問を編集しました。 – MoonKnight

2

DataGridRowすなわち - 私が発見した非常に読みになった後

DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator 
             .ContainerFromItem(rv); 
+0

これは、 'row'に対して' null'を返しています。私は 'rv'をチェックしており、これはnullではありません。このことをもう一度お悔やみして申し訳ありませんが、お時間を頂きありがとうございます。 – MoonKnight

+0

GUI上にまだ行が表示されていない場合にのみ、これは 'null'を返します。あなたが扱っている行の数はあなたのグリッドで 'virtualisation'が有効になっていますか? –

+0

ここにリンク - http://stackoverflow.com/questions/6713365/itemcontainergenerator-containerfromitem-returns-nullがあなたの助けになるかもしれません。 –

0

を私が望んだことをする方法 - 特定の条件に応じて実行時にセルを色づけする。これは、ユーザーがスクロール細胞の色がセルからセルへ移動することが

public static T GetVisualChild<T>(Visual parent) where T : Visual 
{ 
    T child = default(T); 
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent); 
    for (int i = 0; i < numVisuals; i++) 
    { 
     Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); 
     child = v as T; 
     if (child == null) 
      child = GetVisualChild<T>(v); 
     if (child != null) 
      break; 
    } 
    return child; 
} 

public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column) 
{ 
    if (row != null) 
    { 
     DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row); 
     if (presenter == null) 
     { 
      grid.ScrollIntoView(row, grid.Columns[column]); 
      presenter = GetVisualChild<DataGridCellsPresenter>(row); 
     } 
     DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); 
     return cell; 
    } 
    return null; 
} 

public static IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid) 
{ 
    var itemsSource = grid.ItemsSource as IEnumerable; 
    if (null == itemsSource) yield return null; 
    foreach (var item in itemsSource) 
    { 
     var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow; 
     if (null != row) yield return row; 
    } 
} 

注意を必要と私はこれらが関連するユーティリティメソッドです着色に

public void RunChecks() 
{ 
    const int baseColumnCount = 3; 
    for (int i = baseColumnCount; i < dataGrid.Columns.Count; i++) 
    { 
     foreach (DataGridRow row in Utilities.GetDataGridRows(dataGrid)) 
     { 
      if (row != null) 
      { 
       DataGridCell cell = dataGrid.GetCell(row, i); 

       // Start work with cell. 
       Color color; 
       TextBlock tb = cell.Content as TextBlock; 
       string cellValue = tb.Text; 
       if (!CheckForBalancedParentheses(cellValue)) 
        color = (Color)ColorConverter.ConvertFromString("#FF0000"); 
       else 
        color = (Color)ColorConverter.ConvertFromString("#FFFFFF"); 
       row.Background = new SolidColorBrush(color); 
       //cell.Background = new SolidColorBrush(color); 
      } 
     } 
    } 
} 

を行うために使用する方法です! ?これはWPFのもう一つのすばらしい迷惑です。私は本当にこのシンプルなものをなぜそんなに難しくすることができるのか、本当に混乱しています。 MVVM型のパターンを使用する必要があるという提案は、私が驚くほど見つけたものです...

私はこれが他の誰かを助けてくれることを願っています。

1

「Kill​​ercam」の答えは私のために働いたが、私は追加する必要:そう、私はnull参照が届かないと確信している

myDataGrid.UpdateLayout();

をgetcellをメソッドを使用する前に。

すべてのことの痛みの後DataGridHelper

で全体のヘルパークラスの外観を得るために、私は全体のコードを試してみて、私は別のprobem WICHが正しく色の細胞は、スクロール時に変更されたことで、ソリューションが仮想化を可能にすることでした直面モードをStandardに設定します。

VirtualizingStackPanel.IsVirtualizing="True" 
VirtualizingStackPanel.VirtualizationMode="Standard" 

ので、これはデータグリッドセルを反復処理する必要がありますいずれかを助けることを願っています。

0
//the Class that resembles the Datagrid Item schema 

foreach (Class cl in dgDatagrid.Items) 
    { 
     MessageBox.Show(cl.ID.ToString()); 
    } 

this is the most simplest way to read the datagrid data 
most articles misguide such simple things 
+0

これは質問に対する答えを提供しません。十分な[評判](https://stackoverflow.com/help/whats-reputation)があれば、[投稿にコメントする]ことができます(https://stackoverflow.com/help/privileges/comment)。代わりに、[質問者からの明確化を必要としない回答を提供する](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- i-do-代わりに)。 - [レビューから](/レビュー/低品質の投稿/ 17321773) –

関連する問題