2012-08-29 14 views
1

我有一個datagrid和行是不同的顏色,取決於它們中的記錄的狀態(有效是白色,問題是黃金,禁止是紅色)。如何綁定WPF中的DataGrid.HighlightBrush按行

問題是,當行被選中時,它們都會變成統一的顏色,並且無法再確定狀態。我想以類似的方式綁定高亮顏色:

  <DataGrid.RowStyle> 
       <Style TargetType="DataGridRow"> 
        <Setter Property="Background" Value="{Binding Member, Converter={StaticResource MemberBackgroundConverter}}" /> 
        <Setter Property="HighlightBrushKey" Value="{Binding Member, Converter={StaticResource MemberHighlightConverter}}" />       
       </Style> 
      </DataGrid.RowStyle> 

上面的第一個Setter工作。有沒有辦法讓第二個人工作?有沒有什麼辦法可以設置每行的高亮刷新?編輯: 以下是我目前工作的內容。我並不是想說這是最好的方法,只是這種方式有效。

XAML:

<DataGrid.Resources> 
    <SolidColorBrush x:Key="SelectionBackgroundColorKey" /> 

    <LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" StartPoint="0,0" EndPoint="0,1"> 
     <GradientStop Color="{Binding Source={StaticResource SelectionBackgroundColorKey}, Path=Color}" Offset="0.0" /> 
     <GradientStop Color="White" Offset="0.3" /> 
     <GradientStop Color="{Binding Source={StaticResource SelectionBackgroundColorKey}, Path=Color}" Offset="1.0" /> 
    </LinearGradientBrush> 

    <SolidColorBrush x:Key="SelectionTextColorKey" Color="Black" /> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{Binding Source={StaticResource SelectionTextColorKey}, Path=Color}" /> 
</DataGrid.Resources> 

<DataGrid.RowStyle> 
    <Style TargetType="DataGridRow"> 
     <Setter Property="Background" Value="{Binding BindsDirectlyToSource=True, Converter={StaticResource ReservationBackgroundConverter}}" /> 
     <Setter Property="Foreground" Value="{Binding IsNew, Converter={StaticResource IsNewForegroundConverter}}" /> 
    </Style> 
</DataGrid.RowStyle> 

代碼:

private void DataGridReservationsSelectionChanged(object argSender, SelectionChangedEventArgs argEvtArgs) 
    { 
     Reservation LocalReservation; 

     ((SolidColorBrush) dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.SlateGray; 
     ((SolidColorBrush) dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black; 

     LocalReservation = dataGridReservations.SelectedItem as Reservation; 

     if (LocalReservation == null) 
     { 
      return; 
     } 

     if(LocalReservation.IsArrived) 
     { 
      ((SolidColorBrush)dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.ForestGreen; 
      ((SolidColorBrush)dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black; 
      return; 
     } 
     //Is this Reservation a Problem? 
     if (LocalReservation.Member.IsProblem) 
     { 
      ((SolidColorBrush) dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.Goldenrod; 
      ((SolidColorBrush) dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black; 
      return; 
     } 

     //Is this Reservation Banned? 
     if (LocalReservation.Member.IsBanned) 
     { 
      ((SolidColorBrush) dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.Firebrick; 
      ((SolidColorBrush) dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black; 
      return; 
     } 
    } 

這種方式讓我有行未選中的顏色,每個獨立的行和選定的顏色,每個獨立的行集設置。

回答

0

如果使用DataGridTextColumn你可以指定ElementStyle:

<DataGrid.Resources> 
    <Style x:Key="MemberCellStyle" TargetType="{x:Type TextBlock}"> 
     <Setter Property="Background" Value="{Binding Member, Converter={StaticResource MemberBackgroundConverter}}" /> 
    </Style> 
</DataGrid.Resources> 

...

<DataGridTextColumn ... ElementStyle="{StaticResource MemberCellStyle}"> 

背景顏色將尊重如果選擇與否行不管結合。

更新:

您可以使用一個MultiDataTrigger,創造條件2(1對自己的地位和1如果選擇行或沒有)。下面是創建代碼樣式的示例(對於選定的和未選定的條件),但如果需要,它應該很容易轉換爲XAML:

Style cellStyle = new Style(typeof(DataGridCell)); 
MultiDataTrigger triggerNotSelected = new MultiDataTrigger(); 
Condition dataCondition = new Condition(new Binding("[STATUS]", [STATUSVALUE]); 
Condition rowNotSelectedCondition = new Condition(); 
rowNotSelectedCondition.Binding = new Binding 
    { 
      Path = new PropertyPath(DataGridRow.IsSelectedProperty), 
      RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGridRow), 1) 
    }; 
rowNotSelectedCondition.Value = false; 

triggerNotSelected.Conditions.Add(dataCondition); 
triggerNotSelected.Conditions.Add(rowNotSelectedCondition); 
triggerNotSelected.Setters.Add(new Setter(Control.BackgroundProperty, Brushes.Gold)); 

MultiDataTrigger triggerSelected = new MultiDataTrigger(); 
Condition rowSelectedCondition = new Condition(); 
rowSelectedCondition.Binding = new Binding 
    { 
     Path = new PropertyPath(DataGridRow.IsSelectedProperty), 
     RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGridRow), 1) 
    }; 
rowSelectedCondition.Value = true; 

triggerSelected.Conditions.Add(dataCondition); 
triggerSelected.Conditions.Add(rowSelectedCondition); 
triggerSelected.Setters.Add(new Setter(Control.BackgroundProperty, Brushes.Khaki)); 

cellStyle.Triggers.Add(triggerNotSelected); 
cellStyle.Triggers.Add(triggerSelected); 
+0

這不是我要找的。例如,未選中的行是LightRed,因此我希望所選行是BrickRed。我想要改變顏色,但仍然有助於確定狀態。我遇到的問題是行是不同的顏色(紅色,綠色和藍色色調),所以每行的選擇顏色需要不同。 –

+0

已更新的答案。讓我知道,如果這就是你要找的。 – KornMuffin

+0

如果我可以使綁定工作,這看起來最有希望。這是我得到的,它不工作: <條件綁定=「{Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type DataGridRow}},Path = IsSelectedProperty}」Value =「true」/> Unselected工作正常。事實上,你甚至不需要指定IsSelected = false。 –