You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF中为DataGrid添加StackPanel及椭圆不显示问题排查

WPF DataGrid 单元格显示控件文本而非控件、椭圆不显示的解决方法

问题根源

  1. 数据类缺少绑定属性:你的Table类没有定义Color属性,当前XAML中Ellipse.Fill绑定的是窗口类的Color属性,导致所有行共享同一个颜色值,且无法实现每行独立的颜色配置。
  2. 类型不匹配:Ellipse.Fill需要Brush类型值,但你绑定的是字符串格式的颜色代码,WPF无法自动完成类型转换。
  3. 数据类未实现通知接口:Table类未实现INotifyPropertyChanged,后续若需更新数据无法触发UI自动刷新。

解决方案

1. 修改Table数据类,添加Color属性并实现INotifyPropertyChanged

public class Table : INotifyPropertyChanged
{
    public string Tag_Name { get; set; }
    public string Tag_Comment { get; set; }
    public float Value { get; set; }
    public string Unit { get; set; }
    public float Lower { get; set; }
    public float Upper { get; set; }

    private string _color;
    public string Color
    {
        get => _color;
        set
        {
            _color = value;
            OnPropertyChanged(nameof(Color));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

2. 修正FillDataGrid方法,为每行数据分配独立颜色

移除对窗口Color属性的修改,直接为每个Table对象设置对应颜色:

private void FillDataGrid()
{
    datagrid_List.Clear(); // 清空列表避免重复数据
    for (int i = 0; i < ControllersList.Count; i++)
    {
        // 防止颜色数组索引越界
        var colorIndex = i % Colors.Length;
        datagrid_List.Add(new Table 
        {
            Tag_Name = $"  {i + 1}   {ControllersList[i]}",
            Tag_Comment = "",
            Value = 8,
            Unit = "kj/h",
            Lower = 9,
            Upper = 28,
            Color = Colors[colorIndex] // 为当前行数据绑定专属颜色
        });
    }
    dataGrid.ItemsSource = datagrid_List;
}

3. 添加字符串转Brush的转换器,解决类型不匹配问题

3.1 创建转换器类
public class StringToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is string colorString && !string.IsNullOrEmpty(colorString))
        {
            var converter = new BrushConverter();
            return converter.ConvertFromString(colorString);
        }
        return Brushes.Transparent; // 默认返回透明
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
3.2 在XAML中声明转换器资源

在窗口/UserControl的根元素中添加命名空间(替换为你的项目命名空间),并声明转换器:

<Window xmlns:local="clr-namespace:YourProjectNamespace"
        ...>
    <Window.Resources>
        <local:StringToBrushConverter x:Key="StringToBrushConverter"/>
    </Window.Resources>
    <!-- 其他XAML内容 -->
</Window>
3.3 修改Ellipse的Fill绑定
<Ellipse x:Name="myEllipse" Width="10" Height="10" 
         Fill="{Binding Color, Converter={StaticResource StringToBrushConverter}}" />

额外说明

之前出现单元格显示System.Windows.Controls.StackPanel文本,是因为你曾错误使用DataGridTextColumn绑定了StackPanel对象。现在你的XAML已经改用DataGridTemplateColumn,只要数据绑定逻辑正确,就不会再出现该问题。

内容的提问来源于stack exchange,提问作者Mahmoud Hassan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 19:55:20