WPF DataGrid 动态修改某一个单元格的样式

WPF DataGrid 动态修改某一个单元格的样式

csharp 复制代码
 <DataGrid Name="main_datagrid_display" Width="1267" Height="193" Grid.Column="1"ItemsSource="{Binding DataGridModels}">
	<DataGrid.Columns>
     	<!--ElementStyle 设置元素样式-->
     	<DataGridTextColumn Header="状态" Width="*" IsReadOnly="True" Binding="{Binding ZhuangTai}" ElementStyle="{StaticResource textblock_textalignment_center}"></DataGridTextColumn>
 	</DataGrid.Columns>
</DataGrid>

对于 DataGrid 某一行,如果我们想要动态的对指定单元格的样式进行修改,按照上述代码,是很难去实现的,但是有时候需求就需要我们动态的修改指定的单元格,例如我这里动态对指定单元格修改前景色,所对应的绑定数据定义如下:

csharp 复制代码
public class DataGridModel : INotifyPropertyChanged
{
    private System.Windows.Media.Brush _foreColor;
    public System.Windows.Media.Brush ForeColor
    {
        get { return _foreColor; }
        set
        {
            if (_foreColor != value)
            {
                _foreColor = value;
                if (null != PropertyChanged)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ForeColor"));
                }
            }
        }
    }
    public event PropertyChangedEventHandler? PropertyChanged;
    public DataGridModel(System.Windows.Media.Brush color)
    {
        ForeColor = color;
    }
}

方法一:使用 DataGridTemplateColumn

csharp 复制代码
<DataGrid Name="main_datagrid_display" Width="1267" Height="193" Grid.Column="1"ItemsSource="{Binding DataGridModels}">
	<DataGrid.Columns>

	    <!--DataGridTemplateColumn 实现-->
	    <DataGridTemplateColumn Header="状态" Width="*" IsReadOnly="True">
	        <DataGridTemplateColumn.CellTemplate>
	            <DataTemplate>
	                <Border>
	                    <TextBox Text="{Binding ZhuangTai}" Foreground="{Binding ForeColor}" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"></TextBox>
	                </Border>
	            </DataTemplate>
	        </DataGridTemplateColumn.CellTemplate>
	    </DataGridTemplateColumn>
	     <!--DataGridTemplateColumn 实现-->

	</DataGrid.Columns>
</DataGrid>

这里我在 xaml 中为 Foreground 绑定 ForeColor 属性,在后台就可以动态修改其前景色,下同。

方法二:修改 DataGridTextColumn .ElementStyle

csharp 复制代码
<DataGrid Name="main_datagrid_display" Width="1267" Height="193" Grid.Column="1"ItemsSource="{Binding DataGridModels}">
	<DataGrid.Columns>

	    <!--DataGridTextColumn .ElementStyle 实现-->
	    <DataGridTextColumn Header="状态" Width="*" IsReadOnly="True" Binding="{Binding ZhuangTai}">
             <DataGridTextColumn.ElementStyle>
                 <Style TargetType="TextBlock">
                     <Setter Property="Foreground" Value="{Binding ForeColor}"></Setter>
                 </Style>
             </DataGridTextColumn.ElementStyle>
         </DataGridTextColumn>
	     <!--DataGridTextColumn .ElementStyle 实现-->

	</DataGrid.Columns>
</DataGrid>

以上两种方式得到的结果一样,可以根据实际情况再稍微调整下格式:

结语:如果修改变动不至于修改一整个单元格的话,推荐使用方法二。

相关推荐
冰茶_1 天前
WPF特性分析
学习·microsoft·c#·wpf
qq_196055871 天前
最快打包WPF 应用程序
wpf
baivfhpwxf20233 天前
wpf ScaleTransform
wpf
C#_西哥3 天前
wpf stylet框架 关于View与viewmodel自动关联绑定的问题
wpf
wqq10273 天前
WPF 图标原地旋转
wpf
续亮~3 天前
基于Redis实现RAG架构的技术解析与实践指南
java·redis·架构·wpf·springai·文档检索
Pasregret4 天前
09-RocketMQ 深度解析:从原理到实战,构建可靠消息驱动微服务
微服务·wpf·rocketmq
沉到海底去吧Go4 天前
【图片识别分类】如何快速识别照片中的水印文字,对图片进行关键字分类,快速整理水印相机拍摄图片,基于WPF和腾讯OCR的技术实现
数码相机·ocr·wpf
界面开发小八哥4 天前
界面控件DevExpress WPF v25.1新功能预览 - 文档处理类功能升级
c#·wpf·界面控件·devexpress·ui开发
He BianGu4 天前
【WPF-VisionMaster源代码】应用OpenCVSharp仿Vision Master页面开发的软件源代码
图像处理·opencv·c#·wpf·机器视觉·visionmaster·视频处理