将图像添加到数据网格中的按钮 [英] Adding Image to a button in Data Grid

查看:41
本文介绍了将图像添加到数据网格中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我在wpf应用程序中创建一个动态datahrid ...我在动态网格中放置一个删除按钮来删除数据网格中的行....现在我想要在该按钮的位置放置一个图像按钮...下面的代码我已经使用过...但是有一些noresult ...图像没有代替按钮显示



注意:我没有使用任何xaml ...完整的代码只通过c#完成...请不要发布与xaml相关的答案...因为在我的屏幕上我将获得超过5个datagrids ...所以xaml代码将无法在动态创建中工作.......这是我的代码..请帮助我guyzzz





  //  动态添加按钮 
DataGridTemplateColumn dgtcDel = new DataGridTemplateColumn();
FrameworkElementFactory fefDelButton = new FrameworkElementFactory( typeof (Button));
BitmapImage biDeleteIcon = new BitmapImage();

fefDelButton.SetBinding(Button.TagProperty, new Binding( LineItemID));
fefDelButton.AddHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(btnDelete_Click), true );
DataTemplate buttonTemplate = new DataTemplate();
StackPanel stackPanel = new StackPanel();


dgtcDel.Header = D;
dgtcDel.HeaderStyle = grdHeaderStyle;
buttonTemplate.VisualTree = fefDelButton;
dgtcDel.CellTemplate = buttonTemplate;
biDeleteIcon.BeginInit();
biDeleteIcon.UriSource = new Uri( @ .. \Images\Deleteicon.png,UriKind.Relative);
biDeleteIcon.EndInit();

fefDelButton.SetValue(Image.SourceProperty,biDeleteIcon);
fefDelButton.SetValue(Image.VisibilityProperty,Visibility.Visible);
grdCharges.Columns.Add(dgtcDel);

解决方案

我只解决了我自己的问题......

< br $> b $ b

请按照上面的代码动态添加一个utton,请使用以下代码将图像添加到

 FrameworkElementFactory 





 ControlTemplate ctTempExpand = new ControlTemplate(typeof(Button)); 
var image = new FrameworkElementFactory(typeof(Image));

ctTempExpand.VisualTree = image;
image.SetValue(Image.SourceProperty,new BitmapImage(new Uri(@Plus.png,UriKind.RelativeOrAbsolute)));
image.SetValue(Image.HeightProperty,21.0);
image.SetValue(Image.WidthProperty,18.0);
fefDelButton .SetValue(Button.TemplateProperty,ctTempExpand);
fefDelButton .SetValue(Button.ToolTipProperty,Expand);


我解决了我自己的问题...... 


请按照上面的代码动态添加一个字符串,请使用以下代码将图像添加到< pre > FrameworkElementFactory < / pre >

< pre > ControlTemplate ctTempExpand = new ControlTemplate(typeof(Button));
var image = new FrameworkElementFactory(typeof(Image));

ctTempExpand.VisualTree = image;
image.SetValue(Image.SourceProperty,new BitmapImage(new Uri(@Plus.png,UriKind.RelativeOrAbsolute)));
image.SetValue(Image.HeightProperty,21.0);
image.SetValue(Image.WidthProperty,18.0);
fefDelButton .SetValue(Button.TemplateProperty,ctTempExpand);
fefDelButton .SetValue(Button.ToolTipProperty,Expand); < / pre < span class =code-keyword>>


我知道的一种方法是,在网格结构中定义该列的格式化方法。因此,不是以声明方式定义网格的结构,而是在JavaScript对象中定义如下所示



var structure = [

{

名称:名字,

字段:first_name

},

{

名称:动作,

字段:_ item,

格式化程序:function(item){

var btn = new dijit。 form.Button({

标签:编辑

});

返回btn;

} < br $>
}



并将此结构设置为网格




Hello,
I am creating a dynamic datahrid in wpf application... I the dynamic grid i am placing a delete button to delete the rows in a datagrid.... Now i want to place an imagebutton in the place of that button... Below code ihave used... but there is noresult... image is not displaying in place of button

Note: i am not using any of xaml... complete code is done through c# only... Pls dont post answers that are related to xaml... Beacuse in my screen i will get more than 5 datagrids... so xaml code will not work in dynamic creation ....... Here is my code.. Please help me guyzzz


//Adding Button Dynamically
               DataGridTemplateColumn dgtcDel = new DataGridTemplateColumn();
               FrameworkElementFactory fefDelButton = new FrameworkElementFactory(typeof(Button));
               BitmapImage biDeleteIcon = new BitmapImage();

               fefDelButton.SetBinding(Button.TagProperty, new Binding("LineItemID"));
               fefDelButton.AddHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(btnDelete_Click), true);
               DataTemplate buttonTemplate = new DataTemplate();
               StackPanel stackPanel = new StackPanel();


               dgtcDel.Header = "D";
               dgtcDel.HeaderStyle = grdHeaderStyle;
               buttonTemplate.VisualTree = fefDelButton;
               dgtcDel.CellTemplate = buttonTemplate;
               biDeleteIcon.BeginInit();
               biDeleteIcon.UriSource = new Uri(@"..\Images\Deleteicon.png", UriKind.Relative);
               biDeleteIcon.EndInit();

               fefDelButton.SetValue(Image.SourceProperty, biDeleteIcon);
               fefDelButton.SetValue(Image.VisibilityProperty, Visibility.Visible);
               grdCharges.Columns.Add(dgtcDel);

解决方案

I solved it my self only yar......


please follow the above code to add a utton dynamically and please use the below code to add image to that

FrameworkElementFactory



ControlTemplate ctTempExpand = new ControlTemplate(typeof(Button));
               var image = new FrameworkElementFactory(typeof(Image));

               ctTempExpand.VisualTree = image;
               image.SetValue(Image.SourceProperty, new BitmapImage(new Uri(@"Plus.png", UriKind.RelativeOrAbsolute)));
               image.SetValue(Image.HeightProperty, 21.0);
               image.SetValue(Image.WidthProperty, 18.0);
               fefDelButton .SetValue(Button.TemplateProperty,ctTempExpand);
               fefDelButton .SetValue(Button.ToolTipProperty, "Expand");


I solved it my self only yar......


please follow the above code to add a utton dynamically and please use the below code to add image to that <pre>FrameworkElementFactory</pre>

<pre> ControlTemplate ctTempExpand = new ControlTemplate(typeof(Button));
                var image = new FrameworkElementFactory(typeof(Image));

                ctTempExpand.VisualTree = image;
                image.SetValue(Image.SourceProperty, new BitmapImage(new Uri(@"Plus.png", UriKind.RelativeOrAbsolute)));
                image.SetValue(Image.HeightProperty, 21.0);
                image.SetValue(Image.WidthProperty, 18.0);
                fefDelButton .SetValue(Button.TemplateProperty,ctTempExpand);
                fefDelButton .SetValue(Button.ToolTipProperty, "Expand");</pre>


The one way I know is, that defining formatting method for that column in grid structure. So instead of defining the structure of the grid declaratively, define in JavaScript object like below

var structure = [
{
name: "First Name",
field: "first_name"
},
{
name: "Action",
field: "_item",
formatter: function(item){
var btn = new dijit.form.Button({
label: "Edit"
});
return btn;
}
}

and set this structure to the grid


这篇关于将图像添加到数据网格中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆