我正在尋找一種方法,我如何以編程方式將一列顯示圖像添加到數據網格。我一直在尋找如何找到解決方案很長一段時間沒有成功。Datagrid圖像列以編程方式添加wpf
我現在做它的方式是這樣的:
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);
這應該創建一個列在那裏我可以顯示圖片。 所有數據,我從一個數據庫,這是我在colums存儲這樣得到:
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路綁定模式?如果沒有,請將其更改爲單向 – 2013-04-23 15:40:11
我試過這樣做,唯一發生變化的是在輸出窗口中「雙向」到「單向」。 – 2013-04-23 15:44:36
看看[這個答案](http://stackoverflow.com/a/3427387/1466627)修復你的圖像到ImageSource錯誤。 – 2013-04-23 15:46:05