2010-01-12 58 views
2

我正在製作一個WPF程序,它能夠使用for循環以紅色將DataGrid中的行逐個着色,我遇到了一些奇怪的事情。如果DataGrid具有來自數據庫表的超過40行數據,則不會爲所有行着色。着色WPF DataGridRows一個接一個

這是我正在使用的代碼。

private void Red_Click(object sender, RoutedEventArgs e) 
{ 
    for (int i = 0; i < dataGrid1.Items.Count; i++) 
    { 
     DataGridRow row = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(i); 
     if (row != null) 
     { 
      row.Background = Brushes.Red; 
     } 
    } 
} 

是否有任何其他方式通過其他方法逐一着色行,或者這是wpftoolkit中的某種錯誤?

回答

4

如果您想要爲每行定義顏色,並且在行顯示的項目上有一個屬性,您可以使用ItemsContainerStyle設置行顏色。在下面的示例中,您將在網格中的項目上有一個名爲ItemColour的屬性,該屬性將定義背景行顏色。綁定從行綁定到行所包含的項目。

<dg:DataGrid.ItemContainerStyle> 
    <Style 
     TargetType="{x:Type dg:DataGridRow}" 
     BasedOn="{StaticResource {x:Type dg:DataGridRow}}"> 
     <Setter 
      Property="Background" 
      Value="{Binding ItemColour}" /> 
    </Style> 
</dg:DataGrid.ItemContainerStyle> 

但是您可能不希望物品屬性ItemColour對您的物品,因爲它們可能是您的業務模型。這是ViewModel進入它自己的地方。您可以定義一箇中間層,它基於一些自定義邏輯封裝您的業務層和ItemColour屬性。

+0

你能提供一個關於這個ViewModel的例子嗎?當涉及到綁定屬性時,我仍然不太好。 – KenNY 2010-01-13 01:44:17

+1

http://sites.google.com/site/wpfprojects/下載彩色行示例,並將此問題標記爲已回答。可能還有其他一些項目,您可能也會喜歡看看 – 2010-01-13 03:36:25

+0

+1,因爲拼寫的顏色是'u',哦,並且感謝這個例子。 – Grokodile 2010-04-22 15:55:32

0

屏幕上不可見的行將無法使用此方法着色,因爲它們被虛擬化並且實際上不存在。在下面IM綁定屬性的樣式IsRed(從與它的數據網格把這個的資源)

 <Style 
      TargetType="{x:Type dg:DataGridRow}" 
      BasedOn="{StaticResource {x:Type dg:DataGridRow}}"> 
      <Style.Triggers> 
       <DataTrigger 
       Binding="{Binding ElementName=self, Path=IsRed}" 
       Value="True"> 
       <Setter 
        Property="Background" 
        Value="Red" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 

變成紅色,其默認的顏色之間的行我有我的窗體上的依賴屬性所謂IsRed,這也可能是實現INotifyPropertyChanged(依賴屬性通知他們的變化)

public Boolean IsRed { 
    get { return (Boolean)GetValue(IsRedProperty); } 
    set { SetValue(IsRedProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for IsRed. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty IsRedProperty = 
     DependencyProperty.Register("IsRed", typeof(Boolean), typeof(Window1), new UIPropertyMetadata(false)); 

然後在我的XAML我有聲明頂部

<Window 
    x:Class="Grids.Window1" 
    x:Name="self"> 
任何財產

這意味着我可以與元素的名稱綁定引用它(的技術我覺得有用)

隨着代碼爲香港專業教育學院列出您所有的按鈕點擊將要做的是

private void Button_Click(object sender, RoutedEventArgs e) { 
    IsRed = !IsRed; 
    } 
+0

我明白了。如果這些行不存在,因爲它們在數據網格中看不到,這是否意味着無法使用多種顏色對行進行着色(例如:白色/黑色/灰色或黃色/紅色/藍色)?感謝您的答案btw。我仍然覺得必須有其他方法來解決這個問題。 – KenNY 2010-01-12 04:58:44

2

如果你想要爲網格的所有行設置背景,您可以定義一個新的行樣式對象並設置其背景屬性;這應該一次改變所有的行背景,而不需要遍歷它們。水木清華這樣的:

dataGrid1.RowStyle = new Style(typeof(DataGridRow)); 
dataGrid1.RowStyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Red))); 

還有,你需要根據它們背後的數據對象的狀態來改變你的DataGrid的行背景的機會。在這種情況下,您可以在xaml中使用觸發器設置自定義樣式,併爲其分配rowstyle。我想水木清華這樣的:

<Window.Resources> 
    <Style x:Key="customDataGridRowStyle" TargetType="{x:Type DataGridRow}"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Test1}" Value="1"> 
       <Setter Property="Background" Value="Red"/> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
.. 
<DataGrid .. DataGrid.RowStyle ="{StaticResource customDataGridRowStyle}" > 
.. 
上面紅色背景的例子

設爲每當它「Test1的」屬性獲取值「1」

希望這有助於該行,至於