Wpf DataGrid插入列,列的类型问题
在C#winform窗口程序中DataGird插入列允许有按钮或者图片类型的列。wpf程序中默认是没有的,请问wpf的DataGird可以插入按钮列和图片列吗?如果可以,...
在C# winform窗口程序中 DataGird插入列允许有按钮 或者图片类型的列。
wpf程序中默认是没有的,请问wpf的DataGird可以插入 按钮列 和图片列吗?
如果可以,要怎么操作呢? 展开
wpf程序中默认是没有的,请问wpf的DataGird可以插入 按钮列 和图片列吗?
如果可以,要怎么操作呢? 展开
展开全部
默认没提供。但是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>
我给这些列加了绑定的属性,也可以不加!~望对你有帮助^ ^
按钮列:
<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>
我给这些列加了绑定的属性,也可以不加!~望对你有帮助^ ^
展开全部
重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;
}
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询