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>

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

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

相关推荐
fireworkseasycold4 小时前
wpf 基于 JSON 的扩展配置 (Extended Config)” 功能
oracle·json·wpf
脩衜者10 小时前
极其灵活且敏捷的WPF组态控件ConPipe 2026
前端·物联网·ui·wpf
张人玉11 小时前
西门子 S7 PLC 通信 WPF 应用分析笔记
笔记·c#·wpf·plc
张人玉11 小时前
整合 Sugar ORM 连接 SQLite 数据库到 WPF 折线图项目
数据库·sqlite·c#·wpf
bugcome_com2 天前
WPF 中控件样式定义的三种方式详解
wpf
Psycho_MrZhang2 天前
Ray 设计思想总结
wpf
廋到被风吹走3 天前
【Spring】Spring AMQP 详细介绍
java·spring·wpf
kylezhao20193 天前
WinForm/WPF界面开发(常用控件、布局设计、事件处理)
大数据·hadoop·wpf
Psycho_MrZhang4 天前
REST/gRPC/队列通信模式对比
wpf
墨白曦煜4 天前
Seata AT 模式:应用层回滚 vs 引擎层回滚
wpf