简体   繁体   English

C#WPF-将图像添加到数据集或数据网格-仅背后提供代码

[英]C# WPF - Adding an image to a dataset or datagrid - code behind only

I have a datagrid which is defined at the beginning 我有一个在开始时定义的数据网格

 DataGrid dtgWatch;

and linked to a Dataset dtsWatch; 并链接到数据集dtsWatch;

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

in the middle the dataset is filled with rows with 在中间,数据集填充有

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

The purpose is something like the following tab: 目的类似于以下选项卡:

| | string1_A | string1_A | string2_A | string2_A |

| | string1_B | string1_B | string2_B | string2_B |

| | string1_B | string1_B | string2_C | string2_C |

that works properly. 正常工作。 Now the change: at a point of that reasoning (and only for one row) I have to put an image and not a string. 现在进行更改:在该推理的一个点上(仅一行),我必须放置图片而不是字符串。 That means: 这意味着:

| | string1_A | string1_A | string2_A | string2_A |

| | string1_B | string1_B | Image2_B | Image2_B | <--------------- <---------------

| | string1_B | string1_B | string2_C | string2_C |

Theoretically speaking this should work (no compilation error) for in case of the image I am adding the line with 从理论上讲,这应该工作(没有编译错误),以防出现图像的情况

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

but the result is 但结果是

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


A possible solution I have thought of is to put a marker somewhere and then at the runtime (Loading row event or something like that) to change the content of the cell with the image. 我想到的一种可能的解决方案是将标记放在某个位置,然后在运行时(加载行事件或类似事件)更改带有图像的单元格的内容。 Unfortunately I could do that for I could get to directly access and modify the cell. 不幸的是我可以这样做,因为我可以直接访问和修改单元格。


I am doing all that with C# wpf with the appdomain tecnique. 我正在使用带有appdomain tecnique的C#wpf进行所有操作。 That means that I have not xaml, I have to do everything in code behind. 这意味着我还没有xaml,我必须在代码背后做所有事情。 All what I have is a grid to add with the datagrid. 我所拥有的只是一个要与datagrid添加的网格。


I have tried that but it is not completed and all the other solutions I found were implying the use of a xaml 我已经尝试过了但是还没有完成,我发现的所有其他解决方案都暗示着要使用xaml


Thanks for any help 谢谢你的帮助

Patrick 帕特里克

To achieve the desired behaviour you have to create your DataGridColumns manually. 为了实现所需的行为,您必须手动创建DataGridColumns。

You can still write Xaml, because you can easily parse and create the corresponding objects in code behind. 您仍然可以编写Xaml,因为您可以轻松地在后面的代码中解析并创建相应的对象。 See System.Windows.Markup.XamlReader.Load 请参阅System.Windows.Markup.XamlReader.Load

So this should work for you 所以这应该为你工作

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)); 
}

OK I have followed a different approach: 好的,我采用了另一种方法:

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")) };
                }
            }
        }
    }

So instead trying to modify the dtg BEFORE showing it, I change it AFTER. 因此,我尝试在显示之前修改dtg,然后将其更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM