2011-10-08 88 views
0

我有一個WPF DataGrid,我需要在DataGrid中選擇一些行。完成選擇後,藍色高線仍然在行上,我無法弄清楚如何擺脫它。我嘗試點擊其他地方,但藍色的高線仍然在行。如何取消高線WPF DataGrid行?

enter image description here

+0

是否要永久刪除藍色突出顯示的邊框? –

+0

@ RV1987我只是不希望它成爲高線。例如。第三行是高線,但我希望它不像其他行一樣高。我應該在哪裏點擊刪除highline? – KMC

回答

0

雖然我不知道你想做什麼,你可以控制與重新定義DataGridCell和DataGridRow風格海萊。我會告訴你一個例子,這可能是也可能不是你想做的事情。我希望它對你有幫助。

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}"> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" 
         Value="True"> 
        <Setter Property="BorderThickness" 
         Value="0" /> 
       </Trigger> 
       <Trigger Property="IsFocused" 
         Value="False"> 
        <Setter Property="Background" 
         Value="Transparent" /> 
        <Setter Property="Foreground" 
         Value="Black" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <DockPanel> 
     <TextBox DockPanel.Dock="Top"></TextBox> 
     <DataGrid ItemsSource="{Binding}"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="First Name" Binding="{Binding}" /> 
      </DataGrid.Columns> 
     </DataGrid> 
    </DockPanel> 
</Window> 
1

可以處理該事件中隱藏文件代碼IsKeyboardFocusWithinChanged在設置的SelectedItem爲null這樣的 -

private void dg_IsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e) 
{ 
    if (!(bool)e.NewValue) 
    { 
     (sender as DataGrid).SelectedItem = null; 
    } 
} 

XAML文件:

<DataGrid x:Name="dg" IsKeyboardFocusWithinChanged="dg_IsKeyboardFocusWithinChanged"/> 

如果你不想讓在你的數據網格中選擇邊框,你需要重寫System.HighlightBrush並將其添加到你的數據網格資源是這樣的 -

<DataGrid> 
    <DataGrid.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/> 
    </DataGrid.Resources> 
<DataGrid>