【问题标题】:C# WPF - Adding an image to a dataset or datagrid - code behind onlyC# WPF - 将图像添加到数据集或数据网格 - 仅代码后面
【发布时间】:2019-05-02 18:18:47
【问题描述】:

我有一个在开头定义的数据网格

 DataGrid dtgWatch;

并链接到数据集 dtsWatch;

dtgWatch.ItemsSource = dtsWatch.Tables[0].DefaultView;

在数据集的中间填充有行

 dtsWatch.Tables[0].Rows.Add(string1, string2);

目的类似于以下标签:

| string1_A | string2_A |

|字符串1_B | string2_B |

|字符串1_B | string2_C |

正常工作。 现在的变化:在那个推理点(并且仅针对一行)我必须放置一个图像而不是一个字符串。 这意味着:

| string1_A | string2_A |

|字符串1_B |图片2_B |

|字符串1_B | string2_C |

从理论上讲,这应该可以工作(没有编译错误),以防我添加的图像行

Image img =...
dtsWatch.Tables[0].Rows.Add(string1, img);

但结果是

|字符串1 | System.Windows.Controls.Image |


我想到的一个可能的解决方案是在某处放置一个标记,然后在运行时(加载行事件或类似事件)用图像更改单元格的内容。不幸的是,我可以这样做,因为我可以直接访问和修改单元格。


我正在使用 C# wpf 和 appdomain 技术来完成所有这些工作。这意味着我没有xaml,我必须在代码后面做所有事情。我所拥有的只是一个与数据网格一起添加的网格。


我尝试过that,但它没有完成,我发现的所有其他解决方案都暗示使用 xaml


感谢您的帮助

帕特里克

【问题讨论】:

    标签: c# wpf image datagrid dataset


    【解决方案1】:

    要实现所需的行为,您必须手动创建 DataGridColumns。

    您仍然可以编写 Xaml,因为您可以在后面的代码中轻松解析和创建相应的对象。 见System.Windows.Markup.XamlReader.Load

    所以这应该适合你

    dtgWatch.AutoGenerateColumns = false;
    foreach (DataColumn column in dtsWatch.Tables[0].Columns)
    {
        string dataGridTemplateColumn = $@"
        <DataGridTemplateColumn
            Header=""{column.ColumnName}""
            xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
            xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ContentControl Content=""{{Binding {column.ColumnName}}}"" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>";
        XmlReader xr = XmlReader.Create(new StringReader(dataGridTemplateColumn));
        dtgWatch.Columns.Add((DataGridTemplateColumn)System.Windows.Markup.XamlReader.Load(xr)); 
    }
    

    【讨论】:

    • 感谢您的提示,很抱歉迟到了......该解决方案非常有前途,而且它教会了我一种新的技术。也就是说,想象我很愚蠢,但我可以理解它是如何工作的。我会说清楚:同一列有时必须存储字符串,有时必须存储图像。让我们举个例子: Row1: col1=Val1a col2=Val1b Row2: col1:val1 col2= ...来自文件系统的图像...你能给我一个实际的例子吗?谢谢
    • 抱歉,我找到了一条不同的路。不过感谢您的帮助
    【解决方案2】:

    好的,我采用了不同的方法:

    private void Dg_Loaded(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < dg.Items.Count; i++)
        {
            DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
            for (int j = 0; j < dg.Columns.Count; j++)
            {
                TextBlock cellContent = dg.Columns[j].GetCellContent(row) as TextBlock;
                var strContent = cellContent.Text;
                if (strContent == "BBB2")
                {
                        // This if I want merely change the value (not my case)
                        //cellContent.Text = "XXXX";
    
                        //This if I want to put an image
                        var dtgCell = cellContent.Parent as DataGridCell;
                        dtgCell.Content = new Image() { Source = new BitmapImage(new Uri(@"C:\Users\Pictures\Ball_Red.png")) };
                    }
                }
            }
        }
    

    因此,我尝试在显示之前修改 dtg,然后更改它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 2014-07-22
      相关资源
      最近更新 更多