2016-04-11 88 views
0

我正在使用WPF數據網格來顯示數據。我試圖弄清楚如何在單擊項目時突出顯示整個行,如果末尾有空白空間(我正在使用動態生成的列),則包括空白列/空間。請注意,我不想突出顯示特定的單元格。這是我看到的http://imgur.com/7Xjoj2H。該行末尾的未使用空間是當前不存在列的空間,並且未突出顯示。我希望在點擊該行時突出顯示該空間。我試過了:WPF DataGrid突出顯示整行

將寬度設置爲自動的最後一列以填充剩餘空間。這對我不起作用,因爲這樣做會禁用水平滾動條,因爲我使用的是動態創建的列,所以我需要該滾動條。

我也嘗試添加了DataGridRows風格:

<Trigger Property="IsSelected" 
       Value="true"> 
      <Setter Property="Background" 
        Value="{StaticResource SelectedBrush}" /> 
     </Trigger> 

這類作品,雖然,在DataGrid負載,或者當我的程序是沒有焦點,它看起來像這樣:http://imgur.com/EtTmBbH哪裏只有未使用空間的最後一個區域被突出顯示。

如果可能的話,我寧願不要通過在結尾添加一個空白虛擬列來解決此問題,因爲這會導致與將最後一列寬度設置爲自動以上相同的問題。

有什麼建議嗎?

編輯:

下面是DataGridRow樣式代碼:

<Style TargetType="{x:Type DataGridRow}" 
     x:Key="DataGridRowStyle"> 

    <Setter Property="Background" 
      Value="White" /> 

    <Style.Triggers> 
     <Trigger Property="AlternationIndex" 
       Value="1"> 
      <Setter Property="Background" 
        Value="#efefef" /> 
     </Trigger> 

     <Trigger Property="IsMouseOver" 
       Value="true"> 
      <Setter Property="Background" 
        Value="{StaticResource RolloverBrush}" /> 
     </Trigger> 
     <Trigger Property="IsSelected" 
       Value="true"> 
      <Setter Property="Background" 
        Value="{StaticResource SelectedBrush}" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

而選擇刷:

<LinearGradientBrush x:Key="SelectedBrush" 
        StartPoint="0,0" 
        EndPoint="0,1"> 
    <GradientStop Offset="0" 
        Color="#3399FF" /> 
</LinearGradientBrush> 

DataGrid的代碼:

 <DataGrid x:Name="JobListView" 
       AutoGenerateColumns="False" 
       ItemsSource="{Binding UnitStatusCollection, Mode=TwoWay}" 
       CanUserDeleteRows="False" 
       Style="{StaticResource JobGridViewStyle}" 
       SelectedIndex="{Binding JobsListViewSelectedIndex, Mode=TwoWay}" 
       SelectedItem="{Binding JobsListViewSelectedUnitStatusPair}" 
       Utility:DataGridColumnsBehavior.BindableColumns="{Binding DataGridColumns}" 
       ContextMenu="{StaticResource ListViewContextMenu}" 
       Margin="10,0" 
       Grid.Row="1" 
       HorizontalAlignment="Stretch" 
       HorizontalContentAlignment="Stretch" 
       RowStyle="{StaticResource DataGridRowStyle}" 
       AlternationCount="2" 
       HorizontalGridLinesBrush="#5A5A5F" 
       VerticalGridLinesBrush="#5A5A5F"> 
+0

你可以顯示「DataGridRows」的'Style'標籤嗎? –

+0

編輯該帖子以顯示它 – KSF

+0

你如何將它應用到網格?您的代碼適用於DataGrid上的'RowStyle =「{StaticResource DataGridRowStyle}」'。 –

回答

1

你媽Ÿ清晰的默認高亮顏色,讓你的SelectedBrush將用來代替:

<DataGrid ...> 
     <DataGrid.Resources> 
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/> 
      <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#00000000"/> 
     </DataGrid.Resources> 
    </DataGrid> 
+0

我試過了,我將顏色設置爲橙色。現在,當我突出顯示一行時,它看起來像這樣:http://imgur.com/HQH8wvj(我將SolidColorBrush的顏色指定爲橙色)。藍色在那裏,因爲我選擇的畫筆正在繪製它的顏色。我無法弄清楚爲什麼/爲什麼列的處理方式不同於末尾的空白空間,而不考慮行的繪製行爲。 – KSF

+0

似乎DataGrid使用SystemColors.HighlightBrushKey突出顯示現有列佔用的行的一部分。我建議使HighlightBrushKey透明而不是橙色,然後將您的SelectedBrush更改爲您想要的任何顏色。 –

+1

此外,DataGrid中的一行由2部分組成,其中一部分由單元格佔用,第二部分不佔用。您也可以使用相同的SelectedBrush來設置CellStyle。在這種情況下,單元格將以正確的顏色突出顯示。 –

2

爲了使這項工作,我用安東的建議:

此外,在DataGrid中列由兩個部分組成,其中一個被細胞佔據,另一個不是。您也可以使用相同的SelectedBrush來設置CellStyle。在這種情況下,單元格將以正確的顏色突出顯示。

我所做的就是作出DataGridCellStyle風格,並提出了IsSelected觸發:

<Style x:Key="DataGridCellStyle" 
     TargetType="{x:Type DataGridCell}"> 
    <Style.Triggers> 
     <Trigger Property="IsSelected" 
       Value="True"> 
      <Setter Property="Background" 
        Value="{StaticResource SelectedBrush}" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

報告描繪的單元格的背景相同的顏色作爲選定行的背景。這是除了:

<Style TargetType="{x:Type DataGridRow}" 
     x:Key="DataGridRowStyle"> 
    <Style.Triggers> 
     <Trigger Property="IsSelected" 
       Value="true"> 
      <Setter Property="Background" 
        Value="{StaticResource SelectedBrush}" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

這允許繪製的顏色延長負載整個行或當程序失去焦點。

1

我找到了另一種解決方案,這其實簡單一點:

<DataGrid.Resources> 
    <!-- Select everything with orange... --> 
    <SolidColorBrush 
     Color="#ff9933" 
     x:Key="{x:Static SystemColors.HighlightBrushKey}" /> 
    <!-- ...all the time --> 
    <SolidColorBrush 
     Color="#ff9933" 
     x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" /> 
</DataGrid.Resources> 

的問題是整個行突出,但在焦點消失時,「未選擇的」細胞突出了一不同顏色:非活動選擇高亮顯示顏色。

行尾的死區背景由您的IsSelected=True觸發器設置。擺脫這一點,死亡地區將不會「選擇」。對此使用三種不同的顏色,HighlightBrushKeyInactiveSelectionHighlightBrushKey,這一切都變得清晰。

安東丹尼洛夫非常非常接近。但答案並不是讓這些畫筆變得透明,而是讓它們成爲你想要的顏色。