【问题标题】:WPF DataGrid - on row selection/lose focus prevent color changeWPF DataGrid - 行选择/失去焦点防止颜色变化
【发布时间】:2017-04-04 06:26:38
【问题描述】:

我想防止我的程序在数据网格失去焦点并选择其中一个行时更改行的颜色。我现在的代码是:

        <DataGrid.Resources>
            <SolidColorBrush 
            x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" 
                     Color="Red"/>
        </DataGrid.Resources>

我正在寻找类似的东西

Color="保持不变"

【问题讨论】:

    标签: c# .net wpf xaml datagrid


    【解决方案1】:

    下面的代码对我有用...

     <UserControl.Resources>
            <Style x:Key="DataGridCellStyle" TargetType="DataGridCell">
                <!--DISPLAY CONTENT IN MIDDLE-->
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Grid Background="{TemplateBinding Background}">
                                <ContentPresenter VerticalAlignment="Center" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <!--style triggers added to keep the selection color active on lost focus in datagrid-->
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="#005691"/>
                        <Setter Property="Foreground" Value="White"/>
                        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true"/>
                            <Condition Property="Selector.IsSelectionActive" Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="#005691"/>
                        <Setter Property="Foreground" Value="White"/>
                        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
    </UserControl.Resources>
    

    【讨论】:

      【解决方案2】:

      您必须为DataGridCell 样式自定义IsSelected 属性触发器/多触发器,如下所示:

          <Style x:Key="DataGridCellStyle1" TargetType="{x:Type DataGridCell}">
              <Setter Property="Background" Value="Transparent"/>
              <Setter Property="BorderBrush" Value="Transparent"/>
              <Setter Property="BorderThickness" Value="1"/>
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="{x:Type DataGridCell}">
                          <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                              <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                          </Border>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
              <Style.Triggers>
                  <Trigger Property="IsSelected" Value="True">
                      <Setter Property="Background" Value="Red"/>
                      <Setter Property="Foreground" Value="White"/>
                      <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                  </Trigger>
                  <Trigger Property="IsKeyboardFocusWithin" Value="True">
                      <Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
                  </Trigger>
                  <MultiTrigger>
                      <MultiTrigger.Conditions>
                          <Condition Property="IsSelected" Value="true"/>
                          <Condition Property="Selector.IsSelectionActive" Value="false"/>
                      </MultiTrigger.Conditions>
                      <Setter Property="Background" Value="Red"/>
                      <Setter Property="Foreground" Value="White"/>
                      <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                  </MultiTrigger>
                  <Trigger Property="IsEnabled" Value="false">
                      <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                  </Trigger>
              </Style.Triggers>
          </Style>
      

      然后应用您的自定义样式:

              <DataGrid ItemsSource="{Binding Data}" CellStyle="{DynamicResource DataGridCellStyle1}"/>
      


      编辑:添加完整的 XAML:

      MainWindow.xaml:

      <Window x:Class="WpfApplication333.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:WpfApplication333"
          mc:Ignorable="d"
          Title="MainWindow" 
          Height="300" 
          Width="300">
      
      <Window.Resources>
      
          <Style x:Key="DataGridCellStyle1" TargetType="{x:Type DataGridCell}">
              <Setter Property="Background" Value="Transparent"/>
              <Setter Property="BorderBrush" Value="Transparent"/>
              <Setter Property="BorderThickness" Value="1"/>
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="{x:Type DataGridCell}">
                          <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                              <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                          </Border>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
              <Style.Triggers>
                  <Trigger Property="IsSelected" Value="True">
                      <Setter Property="Background" Value="Red"/>
                      <Setter Property="Foreground" Value="White"/>
                      <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                  </Trigger>
                  <Trigger Property="IsKeyboardFocusWithin" Value="True">
                      <Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
                  </Trigger>
                  <MultiTrigger>
                      <MultiTrigger.Conditions>
                          <Condition Property="IsSelected" Value="true"/>
                          <Condition Property="Selector.IsSelectionActive" Value="false"/>
                      </MultiTrigger.Conditions>
                      <Setter Property="Background" Value="Red"/>
                      <Setter Property="Foreground" Value="White"/>
                      <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                  </MultiTrigger>
                  <Trigger Property="IsEnabled" Value="false">
                      <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                  </Trigger>
              </Style.Triggers>
          </Style>
      
      </Window.Resources>
      
      <Window.DataContext>
          <local:MyViewModel/>
      </Window.DataContext>
      
      <Grid>
      
          <DataGrid x:Name="dataGrid" ItemsSource="{Binding Data}" HorizontalAlignment="Left" Margin="8,7,0,0" VerticalAlignment="Top" Height="248" Width="113" CellStyle="{DynamicResource DataGridCellStyle1}"/>
          <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="143,116,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
      
      </Grid>
      

      【讨论】:

      • 我在帖子中添加了完整的 XAML,请参阅我的 EDIT
      • 在您进行编辑之前,我以某种方式设法使其工作,但您的回答完全解决了我的问题。谢谢。
      猜你喜欢
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多