2012-11-20 7 views
5

私は、データグリッドの各行をループし、列の値を引き出し、この値をメソッドに渡し、メソッドの結果に基づいてその行をスタイル設定しようとしています。datarowviewからdatagridrowを取得するWPF

私が見つけた後は、データグリッドの行をループするだけで、私はthisの投稿がどのように可能かを詳述しています。

私はdatarowviewオブジェクトで作業するように少し修正しました。

私が今持っている問題は、

var dgRow = grid.ItemContainerGenerator.ContainerFromItem(r) as DataGridRow; 

は常にnullを返しているということです。

私の場合、なぜこれが起こっているのか、簡単なやり方が分かるのか、誰かに助言してください。

これ以上情報が必要な場合はお知らせください。

HERESに私のコード:

private void colorArchived(DataGrid grid , GX3MaterialSelectionData data) 
    { 
     var row = GetDataGridRows(grid); 
     foreach (DataRowView r in row) 
     { 
      var dgRow = grid.ItemContainerGenerator.ContainerFromItem(r) as DataGridRow; 
      int val = int.Parse(r.Row[0].ToString()); 
      if (data.IsArchived(val)) 
      { 
       // style will be defined in xaml 
       dgRow.Style = mystyle; 
      } 


     } 

    } 

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

答えて

0

あなたは、この場合にStyleSelectorを使用することができます。

public class RowStyle : StyleSelector 
{ 
    public override Style SelectStyle(object item, DependencyObject container) 
    { 
     // here the item property is the entity that the grid row is bound to. 
     // check whatever values you want on it and locate a matching style with 
     // find resource. 

     // return a reference to the correct style here 

     // or allow this to run if you want the default style. 
     return base.SelectStyle(item, container); 
    } 
} 

データグリッドで使用するには、RowStyleSelectorプロパティを設定する必要があります。

<Window x:Class="Rich.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:Rich" 
     DataContext="{Binding RelativeSource={RelativeSource Self}}" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <local:RowStyle x:Key="styleSelector"/> 
    </Window.Resources> 
    <Grid> 
     <DataGrid ItemsSource="{Binding Items}" RowStyleSelector="{StaticResource styleSelector}">    
      <DataGrid.Columns> 
       <DataGridTextColumn Header="test" Binding="{Binding Test1}"/> 
       <DataGridTextColumn Header="test2" Binding="{Binding Test2}"/> 
       <DataGridTextColumn Header="test3" Binding="{Binding Test3}"/> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 
1

は、あなたの質問1として、私はちょうどStyleSelectorクラスは、上記更新しました:

public class RowStyle : StyleSelector 
{ 
    public override Style SelectStyle(object item, DependencyObject container) 
    { 
     var dgRow = item as DataGridRow; 
     int val = int.Parse(dgRow.Row[0].ToString()); 
     if (data.IsArchived(val)) 
     { 
      return Mystyle; 
     } 
     return base.SelectStyle(item, container); 
    } 

    // style will be defined in xaml 
    public Style Mystyle 
    { 
     get; 
     set; 
    } 
} 

注:上記のクラスがアクセスできるように、クラスの静的として「GX3MaterialSelectionDataデータ」を説明直接。

関連する問題