2009-09-30 115 views
3

我想改變WPF Datagrid中第一行(僅)的樣式,但還沒有找到如何去做。我想知道有關創建觸發器,這樣的事情:WPF格式第一行Datagrid

<Style TargetType="{x:Type dg:DataGridRow}"> 
    <Style.Triggers> 
     <Trigger Property="SelectedIndex" Value="0"> 
      <Setter Property="Background" Value="Red"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

當然,這並不工作,因爲沒有「的SelectedIndex」上DataGridRow屬性,但。我也有一些嘗試在我的代碼背後做這件事,但無法讓它工作。

這看起來似乎很簡單,但我沒有管理它,所以任何建議將不勝感激。

感謝, 請問

回答

1

您可能能夠創建的IValueConverter來回報您的風格,無論是作爲一個樣式對象,或者只是一個字符串表示(即該樣式的名稱)。那麼你可以將DataGrid的style屬性綁定到轉換器,並將基礎項目列表作爲參數傳入以確定當前項目的索引?

轉換器可能是這個樣子......

public class StyleConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     Style style1 = App.Current.FindResource("RowStyle1") as Style; 
     Style style2 = App.Current.FindResource("RowStyle2") as Style; 

     List<object> items = parameter as List<object>; 

     if (items[0] == value) 
     { 
      return style1; 
     } 

     return style2; 
    } 
} 

不知道這是否會工作,我可能沒有解釋它也生病!

我很好奇,現在我可以嘗試一下,看看我能否實現它!

+0

嗯 - 是嗎? :) –

+0

我不知道,這是很久以前,在近一年沒有碰過WPF! – TabbyCool