【问题标题】:WPF,s DataGrid insert object as textWPF,s DataGrid 将对象作为文本插入
【发布时间】:2017-05-17 20:15:21
【问题描述】:

我有一个简单的数据网格

<DataGrid x:Name="SmartTable"></DataGrid>

接下来,我在代码中初始化了列,并将其绑定到这个结构体:

public struct HddData
{
    public Ellipse status { set; get; }
    public string id { set; get; }
    public string atribute { set; get; }
    public string current { set; get; }
    public string worst { set; get; }
    public string treshhold { set; get; }
    public string data { set; get; }
}

最后,我正在尝试使用 foreach 在表中插入行:

 Ellipse good = new Ellipse();
 Ellipse bad = new Ellipse();
 good.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF64DD17"));
 bad.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFD50000"));
 SmartTable.Items.Add(new HddData
 {
     status = attr.Value.IsOK ? good : bad,
     id = attr.Value.id,
     atribute = attr.Value.Attribute,
     current = attr.Value.Current.ToString(),
     worst = attr.Value.Worst.ToString(),
     treshhold = attr.Value.Threshold.ToString(),
     data = attr.Value.Data.ToString("X"),
 });

文本数据绑定良好,但 Ellipses 是单元格中的字符串类型,显示如下:

System.Windows.Shapes.Ellipse

我怎样才能不将它们显示为文本,而是显示为椭圆?

【问题讨论】:

  • 学习XAML,这玩意是死路一条。你想要一个 DataGridTemplateColumn
  • @EdPlunkett:+1。 majukin:您需要对数据网格列进行一些模板化。

标签: c# wpf binding datagrid


【解决方案1】:

您不能将EllipseFill 属性绑定到Ellipse。尝试在CellTemplate 中使用ContentControl

<DataGridTemplateColumn Header="test">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel>
                <ContentControl Content="{Binding status}"
                                Width="30" Height="30"
                                HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

【讨论】:

    【解决方案2】:
    public struct HddData
    {
        public Brush status { set; get; }
        public string id { set; get; }
        public string atribute { set; get; }
        public string current { set; get; }
        public string worst { set; get; }
        public string treshhold { set; get; }
        public string data { set; get; }
    }
    
    <DataGridTemplateColumn Header="Status">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <StackPanel>
                    <Ellipse Fill="{Binding status}" Width="10" Height="10" HorizontalAlignment="Center" VerticalAlignment="Center"/>                                
                </StackPanel>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    

    您需要一个Brush 并填充椭圆。

    【讨论】:

    • 似乎是真的,但现在我在单元格中得到了字符串十六进制颜色:)
    • StackPanel 是什么意思?
    • StackPanel 可以包含其他控件,例如textboxlabel 等,因此您可以在Ellipse 旁边添加其他控件。
    猜你喜欢
    • 1970-01-01
    • 2016-08-10
    • 2011-08-12
    • 1970-01-01
    • 2011-01-08
    • 2015-03-26
    • 1970-01-01
    • 2023-02-10
    • 2011-11-19
    相关资源
    最近更新 更多