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>

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

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

相关推荐
code_shenbing4 分钟前
WPF实现打印机控制及打印
wpf
界面开发小八哥21 小时前
界面组件DevExpress WPF中文教程:Grid - 如何显示和隐藏列?
wpf·界面控件·devexpress·ui开发·.net9
虚假程序设计1 天前
python用 PythonNet 从 Python 调用 WPF 类库 UI 用XAML
python·ui·wpf
落落落sss1 天前
MongoDB
数据库·windows·redis·mongodb·微服务·wpf
蒋劲豪1 天前
WPF项目暴露WebApi接口;WinForm项目暴露WebApi接口;C#项目暴露WebApi接口;
开发语言·c#·wpf
狮歌~资深攻城狮2 天前
未来已来:HBase的新功能与发展趋势展望
大数据·wpf·hbase
界面开发小八哥3 天前
界面控件DevExpress WPF v24.2新版亮点:支持.NET 9
.net·wpf·界面控件·devexpress·ui开发·用户界面
九鼎科技-Leo3 天前
WPF快速创建DeepSeek本地自己的客户端-基础思路版本
wpf
MasterNeverDown4 天前
WPF 中为 Grid 设置背景图片全解析
大数据·hadoop·wpf
苏克贝塔4 天前
WPF8-常用控件
wpf