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 小时前
12.NModbus4在C#上的部署与使用 C#例子 WPF例子
开发语言·c#·wpf
我要打打代码1 小时前
在WPF项目中使用阿里图标库iconfont
wpf
拾忆,想起1 天前
Redisson 分布式锁的实现原理
java·开发语言·分布式·后端·性能优化·wpf
weixin_464078071 天前
wpf依赖注入驱动的 MVVM实现(含免费源代码demo)
wpf
beyond谚语1 天前
一、WPF入门介绍+Grid和StackPanel布局介绍+实战模拟Notepad++页面布局
wpf
CPU不够了1 天前
WPF常见问题清单
wpf·自适应
beyond谚语1 天前
二、WPF——Style样式玩法(通过资源字典将Style独立,全局调用)
wpf
光辉岁月~2 天前
使用CalcBinding实现复杂逻辑绑定
wpf
SunflowerCoder2 天前
WPF迁移avalonia之触发器
c#·wpf·avalonia
主宰者2 天前
【WPF+Prism】日常开发问题总结
wpf