Wpf DataGrid插入列,列的类型问题

在C#winform窗口程序中DataGird插入列允许有按钮或者图片类型的列。wpf程序中默认是没有的,请问wpf的DataGird可以插入按钮列和图片列吗?如果可以,... 在C# winform窗口程序中 DataGird插入列允许有按钮 或者图片类型的列。
wpf程序中默认是没有的,请问wpf的DataGird可以插入 按钮列 和图片列吗?
如果可以,要怎么操作呢?
展开
 我来答
Mickal小米
推荐于2016-07-09 · TA获得超过3666个赞
知道大有可为答主
回答量:1542
采纳率:100%
帮助的人:1771万
展开全部
默认没提供。但是wpf提供了模板列,它允许你设置任何控件类型的列,具体操作如下。
按钮列:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="Button_Click" Tag="{Binding Id}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
图片列:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding ImageSource}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我给这些列加了绑定的属性,也可以不加!~望对你有帮助^ ^
xiangjuan314
2016-03-04 · TA获得超过3.3万个赞
知道大有可为答主
回答量:2.9万
采纳率:0%
帮助的人:2868万
展开全部
重DataGridTextBoxColumn类

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Collections;

namespace ImageCellInDataGrid
{
/// <summary>
/// Summary description for DataGridImageCell.
/// </summary>
public class DataGridImageCell : DataGridTextBoxColumn
{
private ArrayList theImages1;
public DataGridImageCell()
{

}
public ArrayList theImages
{
get{return theImages1;}
set{theImages1 = value;}
}

protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
{
object o = this.GetColumnValueAtRow(source, rowNum);
if(o != null)
{
int i = (int)o;
g.FillRectangle(backBrush, bounds);

Bitmap bmp = (Bitmap)theImages[i];

//GridImageCellDrawOption cellDrawOption = GridImageCellDrawOption.NoResize;
//GridImageCellDrawOption cellDrawOption = GridImageCellDrawOption.FitProportionally;
GridImageCellDrawOption cellDrawOption = GridImageCellDrawOption.FitToCell;

System.Drawing.GraphicsUnit gu = System.Drawing.GraphicsUnit.Point;

RectangleF srcRect = bmp.GetBounds(ref gu);
Rectangle destRect = Rectangle.Empty;

Region saveRegion = g.Clip;

switch(cellDrawOption)
{
case GridImageCellDrawOption.FitToCell:
destRect = bounds;
break;
case GridImageCellDrawOption.NoResize:
destRect = new Rectangle(bounds.X, bounds.Y, (int) srcRect.Width, (int) srcRect.Height);
g.Clip = new Region(bounds);
break;
case GridImageCellDrawOption.FitProportionally:
{
float srcRatio = srcRect.Width / srcRect.Height;
float tarRatio = (float) bounds.Width / bounds.Height;
destRect = bounds;
if( tarRatio < srcRatio )
{
destRect.Height = (int) (destRect.Width * srcRatio);
}
else
{
destRect.Width = (int) (destRect.Height * srcRatio);
}
}
break;

default:
break;
}

if(!destRect.IsEmpty)
g.DrawImage(bmp, destRect, srcRect, gu);

g.Clip = saveRegion;
}
}

protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
//overridden to avoid editing
}

public enum GridImageCellDrawOption
{
FitToCell = 0,
NoResize = 1,
FitProportionally = 2,
};
}
}
测试代码

private System.Windows.Forms.DataGrid dataGrid1;

private void Form1_Load(object sender, System.EventArgs e)
{

//create a datatable
DataTable dt = new DataTable("MyTable");

dt.Columns.Add(new DataColumn("Col0"));
dt.Columns.Add(new DataColumn("Images",typeof(int)));

Random r = new Random();
for(int i = 0; i < nRows; ++i)
{
DataRow dr = dt.NewRow();
dr[0] = string.Format("row{0}", i);
dr[1] = r.Next(4);
dt.Rows.Add(dr);
}

//store bitmaps in an arraylist
bitMaps = new ArrayList();
System.IO.Stream strm = GetType().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Blue hills.jpg");
bitMaps.Add(new Bitmap(strm));
strm = GetType().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Sunset.jpg");
bitMaps.Add(new Bitmap(strm));
strm = GetType().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Water lilies.jpg");
bitMaps.Add(new Bitmap(strm));
strm = GetType().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Winter.jpg");
bitMaps.Add(new Bitmap(strm));

DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "MyTable";

DataGridTextBoxColumn tbc = new DataGridTextBoxColumn();
tbc.MappingName = "Col0";
tbc.HeaderText = "Column 1";
tbc.Width = 50;
tableStyle.GridColumnStyles.Add(tbc);

int width = this.dataGrid1.ClientSize.Width - tbc.Width - this.dataGrid1.RowHeaderWidth - 4;
DataGridImageCell tbc1 = new DataGridImageCell();
tbc1.MappingName = "Images";
tbc1.HeaderText = "Images";
tbc1.theImages = bitMaps;
tbc1.Width = width;
tableStyle.GridColumnStyles.Add(tbc1);

this.dataGrid1.TableStyles.Add(tableStyle);
this.dataGrid1.DataSource = dt;
dt.DefaultView.AllowNew = false;

Rectangle rect = this.dataGrid1.GetCellBounds(0,0);
topPos = rect.Top;
int height = (this.dataGrid1.ClientSize.Height - topPos - nRows) / nRows;
tableStyle.PreferredRowHeight = height;
this.dataGrid1.DataSource = null;
this.dataGrid1.DataSource = dt;

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式