簡體   English   中英

以編程方式在Datagrid模板中添加圖像c#

[英]programatically add a image in Datagrid template c#

以下xaml代碼將“Image”與datagrid模板列綁定。

     <DataGridTemplateColumn Header="">

       <DataGridTemplateColumn.CellTemplate>

           <DataTemplate>

                <Image Source="{Binding Path=Image}" Height="16" Width="16" VerticalAlignment="Top" />

             </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>

   The same thing need to be done in codebehind (c# code)  

       DataGridTemplateColumn im = new DataGridTemplateColumn();
       im.Header = "";
       Binding items1 = new Binding();

這就是我試過的......如何將datagridtemplate列綁定到圖像?

下面是以編程方式添加DataGridTemplateColumn的代碼。

http://social.msdn.microsoft.com/Forums/en/wpf/thread/df77a277-91d4-41f1-a42a-0fa02a443ff4

DataGridTemplateColumn imgColumn = new DataGridTemplateColumn();
imgColumn.Header = "Image";

FrameworkElementFactory imageFactory = new FrameworkElementFactory(typeof(Image));
imageFactory.SetBinding(Image.SourceProperty, new Binding("ImgPath"));

DataTemplate dataTemplate = new DataTemplate();
dataTemplate.VisualTree = imageFactory;

imgColumn.CellTemplate = dataTemplate;

DGImages.Columns.Add(imgColumn);

我還添加了我創建的示例應用程序的源代碼。 希望能幫助到你。

MainWindow.xaml文件

<Window x:Class="StackOverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Name="DGImages" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Image Desc" Binding="{Binding ImgDesc}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>

MainWindow.xaml.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace StackOverflow
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<SampleClass> imgList = null;

public MainWindow()
{
InitializeComponent();
imgList = new ObservableCollection<SampleClass>();
imgList.Add(new SampleClass() { ImgDesc = "First Image", ImgPath = @"/Images/MyImage.jpg" });

DataGridTemplateColumn imgColumn = new DataGridTemplateColumn();
imgColumn.Header = "Image";

FrameworkElementFactory imageFactory = new FrameworkElementFactory(typeof(Image));
imageFactory.SetBinding(Image.SourceProperty, new Binding("ImgPath"));

DataTemplate dataTemplate = new DataTemplate();
dataTemplate.VisualTree = imageFactory;

imgColumn.CellTemplate = dataTemplate;

DGImages.Columns.Add(imgColumn);

this.DGImages.ItemsSource = imgList;
}
}
}

SampleClass.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace StackOverflow
{
public class SampleClass : INotifyPropertyChanged
{
private string _ImgDesc;

public string ImgDesc
{
get { return _ImgDesc; }
set
{
_ImgDesc = value;
OnPropertyChanged("ImgDesc");
}
}

private string _ImgPath;

public string ImgPath
{
get { return _ImgPath; }
set
{
_ImgPath = value;
OnPropertyChanged("ImgPath");
}
}

public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}

你能嘗試改變嗎?

<DataGridTemplateColumn Header="Image">

<Image Source="{Binding Image}" /> maybe this one 2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM