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>

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

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

相关推荐
雁于飞18 小时前
分布式基础
java·spring boot·分布式·spring·wpf·cloud native
oioihoii1 天前
WPF入门指南:解析默认项目结构
wpf
极客智造1 天前
深入解析 ReactiveUI:WPF 响应式 MVVM 开发的 “终极方案”
wpf
Macbethad3 天前
使用WPF编写一个多维度伺服系统的程序
大数据·hadoop·wpf
lingxiao168883 天前
WPF Prism框架应用
c#·wpf·prism
Macbethad3 天前
使用WPF编写一个Ethercat主站的程序
wpf
难搞靓仔3 天前
WPF 弹出窗体Popup
wpf·popup
Macbethad3 天前
使用WPF编写一个MODBUSTCP通信的程序
wpf
unicrom_深圳市由你创科技3 天前
Avalonia.WPF 跨平台图表的使用
wpf
-大头.4 天前
深入解析ZooKeeper核心机制
分布式·zookeeper·wpf