简体   繁体   中英

Datagrid image column add programatically wpf

I'm looking for a way on how I can add a column, that shows images, to a datagrid programatically. I've been looking on how to find the solution for a long time with no success.

The way I'm doing it now is like this:

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

This should create a column where I can show images. All the data I get from a database, which I store in colums like this:

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 is an own made class where I store in all my bindings:

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

What I get now is absolutely nothing. I don't get any errors though.

So my question is: how can I store the image into the datagrid without having to mess around with the xaml file too much.

Output:

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')

You should use the SetBinding method instead of SetValue , since you are assigning the binding to the property, not assigning a value to the property.

From:

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

To:

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

Your binding statement can also be written as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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