WPF中自定义DataGrid表格后,修改选中行的字体颜色或背景图

发布于:2025-06-21 ⋅ 阅读:(14) ⋅ 点赞:(0)

xaml:

        <ScrollViewer Height="1600" Width="1000" Margin="0 100 0 0" VerticalAlignment="Top" ManipulationBoundaryFeedback="SCManipulationBoundaryFeedback"  VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden">
            <DataGrid Name="DataGrid1" AutoGenerateColumns="False" HorizontalAlignment="Center" VerticalAlignment="Top" Width="1000" IsReadOnly="True" RowHeaderWidth="0" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" HorizontalGridLinesBrush="#FFE2E2E2" VerticalGridLinesBrush="#FFD6D6D6" BorderBrush="LightGray" ColumnHeaderHeight="65" RowHeight="60" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" SelectionChanged="DataGrid1_SelectionChanged" >
                <DataGrid.RowStyle>
                    <Style TargetType="{x:Type DataGridRow}">
                        <Setter Property="Margin" Value="0 0 0 0"/>
                        <Setter Property="Background" Value="Transparent"/>
                        <Setter Property="Height" Value="60"/>
                        <Setter Property="FontSize" Value="24"/>
                        <Setter Property="VerticalContentAlignment" Value="Center"/>
                    </Style>
                </DataGrid.RowStyle>

                <DataGrid.Columns>

                    <DataGridTemplateColumn Header="Id" Width="*" Visibility="Hidden" >
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="DataGridColumnHeader">
                                <Setter Property="HorizontalContentAlignment" Value="Center" />
                                <Setter Property="Foreground" Value="#000" />
                                <Setter Property="FontSize" Value="26" />
                                <Setter Property="Height" Value="60" />
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>


                        <DataGridTemplateColumn.CellStyle>
                            <Style TargetType="{x:Type DataGridCell}">
                                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                                <Setter Property="BorderThickness" Value="0"/>
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="Foreground" Value="#000"/>
                                <Setter Property="VerticalContentAlignment" Value="Center"/>
                            </Style>
                        </DataGridTemplateColumn.CellStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Id}" TextAlignment="Center" VerticalContentAlignment="Center" Foreground="#999" FontSize="24" BorderThickness="0" IsReadOnly="True" Style="{x:Null}"  Margin="2 0 2 0" >
                                </TextBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>


                    <DataGridTemplateColumn Header="设备名称" Width="*" >
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="DataGridColumnHeader">
                                <Setter Property="HorizontalContentAlignment" Value="Center" />
                                <Setter Property="Foreground" Value="#000" />
                                <Setter Property="FontSize" Value="26" />
                                <Setter Property="Height" Value="60" />
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>


                        <DataGridTemplateColumn.CellStyle>
                            <Style TargetType="{x:Type DataGridCell}">
                                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                                <Setter Property="BorderThickness" Value="0"/>
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="Foreground" Value="#000"/>
                                <Setter Property="VerticalContentAlignment" Value="Center"/>
                            </Style>
                        </DataGridTemplateColumn.CellStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Label Content="{Binding DeviceName}" Foreground="#999" FontSize="24" BorderThickness="0" Style="{x:Null}"  Margin="2 0 2 0" InputMethod.IsInputMethodEnabled="False" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
                                </Label>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn Header="COM口" Width="*" >
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="DataGridColumnHeader">
                                <Setter Property="HorizontalContentAlignment" Value="Center" />
                                <Setter Property="Foreground" Value="#000" />
                                <Setter Property="FontSize" Value="26" />
                                <Setter Property="Height" Value="60" />
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>


                        <DataGridTemplateColumn.CellStyle>
                            <Style TargetType="{x:Type DataGridCell}">
                                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                                <Setter Property="BorderThickness" Value="0"/>
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="Foreground" Value="#000"/>
                                <Setter Property="VerticalContentAlignment" Value="Center"/>
                            </Style>
                        </DataGridTemplateColumn.CellStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Label Content="{Binding COM}" Foreground="#999" FontSize="24" BorderThickness="0" Style="{x:Null}"  Margin="2 0 2 0" InputMethod.IsInputMethodEnabled="False" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
                                </Label>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn Header="波特率" Width="*" >
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="DataGridColumnHeader">
                                <Setter Property="HorizontalContentAlignment" Value="Center" />
                                <Setter Property="Foreground" Value="#000" />
                                <Setter Property="FontSize" Value="26" />
                                <Setter Property="Height" Value="60" />
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>


                        <DataGridTemplateColumn.CellStyle>
                            <Style TargetType="{x:Type DataGridCell}">
                                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                                <Setter Property="BorderThickness" Value="0"/>
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="Foreground" Value="#000"/>
                                <Setter Property="VerticalContentAlignment" Value="Center"/>
                            </Style>
                        </DataGridTemplateColumn.CellStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Label Content="{Binding Baud}" Foreground="#999" FontSize="24" BorderThickness="0" Style="{x:Null}"  Margin="2 0 2 0" InputMethod.IsInputMethodEnabled="False" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
                                </Label>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>


                    <DataGridTemplateColumn Header="类型" Width="120" >
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="DataGridColumnHeader">
                                <Setter Property="HorizontalContentAlignment" Value="Center" />
                                <Setter Property="Foreground" Value="#000" />
                                <Setter Property="FontSize" Value="26" />
                                <Setter Property="Height" Value="60" />
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>


                        <DataGridTemplateColumn.CellStyle>
                            <Style TargetType="{x:Type DataGridCell}">
                                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                                <Setter Property="BorderThickness" Value="0"/>
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="Foreground" Value="#000"/>
                                <Setter Property="VerticalContentAlignment" Value="Center"/>
                            </Style>
                        </DataGridTemplateColumn.CellStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Label Content="{Binding Type}" Foreground="#999" FontSize="24" BorderThickness="0" Style="{x:Null}"  Margin="2 0 2 0" InputMethod.IsInputMethodEnabled="False" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
                                </Label>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>


                    <DataGridTemplateColumn Header="创建时间" Width="250" Visibility="Hidden">
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="DataGridColumnHeader">
                                <Setter Property="HorizontalContentAlignment" Value="Center" />
                                <Setter Property="Foreground" Value="#000" />
                                <Setter Property="FontSize" Value="26" />
                                <Setter Property="Height" Value="60" />
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>


                        <DataGridTemplateColumn.CellStyle>
                            <Style TargetType="{x:Type DataGridCell}">
                                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                                <Setter Property="BorderThickness" Value="0"/>
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="Foreground" Value="#000"/>
                                <Setter Property="VerticalContentAlignment" Value="Center"/>
                            </Style>
                        </DataGridTemplateColumn.CellStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Label Content="{Binding  CreateTime,StringFormat='yyyy-MM-dd HH:mm:ss'}" Foreground="#999" FontSize="24" BorderThickness="0" Style="{x:Null}"  Margin="2 0 2 0" InputMethod.IsInputMethodEnabled="False" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
                                </Label>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn Header="更新时间" Width="250">
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="DataGridColumnHeader">
                                <Setter Property="HorizontalContentAlignment" Value="Center" />
                                <Setter Property="Foreground" Value="#000" />
                                <Setter Property="FontSize" Value="26" />
                                <Setter Property="Height" Value="60" />
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>


                        <DataGridTemplateColumn.CellStyle>
                            <Style TargetType="{x:Type DataGridCell}">
                                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                                <Setter Property="BorderThickness" Value="0"/>
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="Foreground" Value="#000"/>
                                <Setter Property="VerticalContentAlignment" Value="Center"/>
                            </Style>
                        </DataGridTemplateColumn.CellStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>

                                <Grid>
                                    <TextBox Text="{Binding UpdateTime,StringFormat='yyyy-MM-dd HH:mm:ss'}" Foreground="#999" FontSize="24" BorderThickness="0" Style="{x:Null}"  Margin="2 0 2 0" InputMethod.IsInputMethodEnabled="False" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                                    <Label Height="60"/>
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                 
                </DataGrid.Columns>
            </DataGrid>
        </ScrollViewer>

cs:

 private void SCManipulationBoundaryFeedback(object sender, ManipulationBoundaryFeedbackEventArgs e)
 {
     e.Handled = true;
 }
  

 private void DataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     foreach (var item in e.RemovedItems)
     {
         var row = DataGrid1.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
         if (row != null)
         {
             UI_Utils.ChangeLabelForeground(row, (SolidColorBrush)(new BrushConverter().ConvertFrom("#999")));
             UI_Utils.ChangeTextBoxsForeground(row, (SolidColorBrush)(new BrushConverter().ConvertFrom("#999")));
         }
     }

     foreach (var item in e.AddedItems)
     {
         var row = DataGrid1.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
         if (row != null)
         {
             UI_Utils.ChangeLabelForeground(row, (SolidColorBrush)(new BrushConverter().ConvertFrom("#3CAAE5")));
             UI_Utils.ChangeTextBoxsForeground(row, (SolidColorBrush)(new BrushConverter().ConvertFrom("#3CAAE5")));
         }
     }
 }

utils:

  public class UI_Utils
  {
      // 辅助方法:改变行中所有TextBlock的前景色
      public static void ChangeTextBlocksForeground(DataGridRow row, Brush foreground)
      {
          var textBlocks = FindVisualChildren<TextBlock>(row);
          foreach (var textBlock in textBlocks)
          {
              textBlock.Foreground = foreground;
          }
      }
      
      
      public static void ChangeTextBoxsForeground(DataGridRow row, Brush foreground)
      {
          var textBoxs = FindVisualChildren<TextBox>(row);
          foreach (var textBox in textBoxs)
          {
              textBox.Foreground = foreground;
          }
      }
      
      
      public static void ChangeLabelForeground(DataGridRow row, Brush foreground)
      {
          var labels = FindVisualChildren<Label>(row);
          foreach (var label in labels)
          {
              label.Foreground = foreground;
          }
      }

      public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
      {
          if (depObj != null)
          {
              for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
              {
                  DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                  if (child != null && child is T)
                  {
                      yield return (T)child;
                  }

                  foreach (T childOfChild in FindVisualChildren<T>(child))
                  {
                      yield return childOfChild;
                  }
              }
          }
      }
  }


网站公告

今日签到

点亮在社区的每一天
去签到