2015-04-24 28 views
0

我使用了WPF數據網格顯示數據,並且當用戶選擇了行,我想整個行的背景被加亮(用梯度),並且還具有邊框。我一直在使用下面的代碼,其中大部分的作品:WPF數據網格DataGridRow選擇邊框和間距

<Style TargetType="DataGridRow"> 
     <Setter Property="BorderBrush" Value="Transparent" /> 
     <Setter Property="BorderThickness" Value="0" /> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding IsChecked}" Value="True"> 
      <Setter Property="BorderThickness" Value="1"/> 
      <Setter Property="BorderBrush" Value="{StaticResource BorderColor}" /> 
      <Setter Property="Background" Value="{StaticResource BackgroundColor}" /> 
     </DataTrigger> 
    </Style.Triggers> 
    </Style> 

我遇到的這個問題是與邊框。如果BorderThickness初始設置爲0,則在觸發DataTrigger時,整個Row會「移位」以爲邊框留出空間。如果我設置borderThickness 1開始,然後突出顯示的行顯示正常,但周圍有行的空邊框時,它在它的默認狀態,導致行網格線不要接觸到邊緣。

對我怎麼能解決這個任何想法?

回答

0

我發現,調整視覺效果與ListBox代替的DataGrid的,所以也許要容易得多,可能是一條路可走。

嘗試以此爲出發點:

<ListBox> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding IsChecked}" Value="True"> 
        <Setter Property="BorderBrush" Value="{StaticResource BorderColor}" /> 
        <Setter Property="Background" Value="{StaticResource BackgroundColor}" /> 
       </DataTrigger> 
      </Style.Triggers> 
      <Setter Property="Background" Value="Transparent" /> 
      <Setter Property="BorderBrush" Value="Transparent" /> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ListBox.ItemTemplate> 
     <DataTemplate DataType="{x:Type local:MyClass}"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="30" /> 
        <ColumnDefinition Width="*" /> 
       </Grid.ColumnDefinitions> 
       <CheckBox IsChecked="{Binding IsChecked}" /> 
       <TextBlock Text="{Binding MyTextProperty}" Grid.Column="1" /> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

這應該設置正確的邊界和背景不移動所選擇的行的元件或表示用於未選定的周圍的間隙。 只需將Class:MyClass替換爲您的類,然後爲您的方案自定義Grid內容。

請參閱this answer關於如何處理MouseOver /選擇樣式,我試着將模板屬性設置器複製到我上面的示例中,並調整Panel.Background和Border.BorderBrush設置器,這似乎很好。

+0

我甚至沒有想到嘗試其他控制。謝謝你的幫助! – bdan