【问题标题】:Datagrid image column add programatically wpfDatagrid图像列以编程方式添加wpf
【发布时间】:2013-04-23 14:45:25
【问题描述】:

我正在寻找一种方法来以编程方式将显示图像的列添加到数据网格。我一直在寻找如何找到解决方案,但没有成功。

我现在的做法是这样的:

DataGridTemplateColumn col1 = new DataGridTemplateColumn();
col1.Header = "Betaald";
FrameworkElementFactory factory1 = new FrameworkElementFactory(typeof(System.Windows.Controls.Image));
Binding b1 = new Binding("Picture") { Mode = BindingMode.TwoWay };
factory1.SetBinding(System.Windows.Controls.Image.SourceProperty, b1);
DataTemplate cellTemplate1 = new DataTemplate();
cellTemplate1.VisualTree = factory1;
col1.CellTemplate = cellTemplate1;
dtgVerkoopsdocumenten.Columns.Add(col1);

这应该会创建一个我可以显示图像的列。 我从数据库中获得的所有数据,我存储在这样的列中:

System.Windows.Controls.Image image = new System.Windows.Controls.Image(); //this is on top of my class

MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
    string naam = getKlant(reader.GetInt32(2));

    if (reader.GetBoolean(7) == false)
    {
        BitmapImage betalen = new BitmapImage(new Uri("/WpfApplication1;component/Images/false.png", UriKind.Relative));
        image.Source = betalen;
    }
    else
    {
        BitmapImage betalen = new BitmapImage(new Uri("/WpfApplication1;component/Images/true.png", UriKind.Relative));
        image.Source = betalen;
    }

    dtgVerkoopsdocumenten.Items.Add(new DataItem
    {
        ID = reader.GetInt32(1),
        klant = naam,
        netto = reader.GetDouble(3),
        btw = reader.GetDouble(4),
        bruto = reader.GetDouble(5),
        datum = reader.GetDateTime(6).ToString("dd-MM-yyyy"),
        soort2 = soort,
        Picture = image
        });
    }
    reader.Close();

DataItem 是一个我自己创建的类,我存储在我的所有绑定中:

//more code, but the one below is the one I'm talking about
public System.Windows.Controls.Image Picture { get; set; }

我现在得到的绝对没有。不过我没有收到任何错误。

所以我的问题是:如何将图像存储到数据网格中,而不必过多地处理 xaml 文件。

输出:

System.Windows.Data Error: 1 : Cannot create default converter to perform 'two-way' conversions between types 'System.Windows.Controls.Image' and 'System.Windows.Media.ImageSource'. Consider using Converter property of Binding. BindingExpression:Path=Picture; DataItem='DataItem' (HashCode=435249); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='System.Windows.Controls.Image' BindingExpression:Path=Picture; DataItem='DataItem' (HashCode=435249); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')
System.Windows.Data Error: 1 : Cannot create default converter to perform 'two-way' conversions between types 'System.Windows.Controls.Image' and 'System.Windows.Media.ImageSource'. Consider using Converter property of Binding. BindingExpression:Path=Picture; DataItem='DataItem' (HashCode=45279885); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='System.Windows.Controls.Image' BindingExpression:Path=Picture; DataItem='DataItem' (HashCode=45279885); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')

【问题讨论】:

  • 您需要它作为 2-Way 绑定模式吗?如果没有,请将其更改为 One-Way
  • 我试过这样做,唯一改变的是输出窗口中的“双向”到“单向”。
  • 看看this answer,它修复了您的 Image to ImageSource 错误。

标签: c# wpf


【解决方案1】:

您应该使用SetBinding 方法而不是SetValue,因为您将绑定分配给属性,而不是为属性分配值。

发件人:

factory1.SetValue(System.Windows.Controls.Image.SourceProperty, b1);

收件人:

factory1.SetBinding(System.Windows.Controls.Image.SourceProperty, b1);

你的绑定声明也可以写成:

Binding b1 = new Binding("Picture") { Mode = BindingMode.TwoWay };

【讨论】:

  • 我的数据网格中仍然一无所获。
  • @RobbeVanWinckel 输出窗口中什么都没有?
  • 检查主帖以查看输出。
猜你喜欢
  • 1970-01-01
  • 2010-10-16
  • 1970-01-01
  • 2010-12-29
  • 2012-11-11
  • 1970-01-01
  • 2015-10-29
  • 2018-05-26
  • 1970-01-01
相关资源
最近更新 更多