2012-11-20 81 views
5

我想通過datagrid中的每一行進行循環,拉出一個列值,將該值傳遞給基於方法結果的行的方法和樣式。從數據視圖獲取datagridrow WPF

找到後我不能循環訪問數據網格的行,我發現this後詳細說明它是如何可能的。

我稍微修改了一下,以便使用datarowview對象。

這個問題我現在已經是

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

總是返回null。

請有人建議爲什麼這是發生在我的情況,如果他們能看到一個更簡單的方法做到這一點。

如果您需要更多信息,請讓我知道。

繼承人我的代碼:

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

根據您的問題,我剛剛更新上述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數據」爲靜態的類,因此,上述類可以訪問它直。