Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
i am adding a button to data grid dynamically... I want to add image button ... so i have followed this code... byut it is not working... so anyone please provide code to me for binding image to that button
C#
<big>//Adding Button Dynamically</big>
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();

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);
Posted
Comments
Marvin Ma 24-Oct-13 3:25am    
DO you have to do it in code or is XAML also possible?
jing567 24-Oct-13 4:47am    
Hello,
I has to do only in code... There is no possibilty for xaml as I am dynamically building it...

1 solution

If you are following mvvm pattern and creating your datagrid in xaml, then you can create a style, which will apply to datagrid buttons. Inside that style you can do:
C#
<setter property="ImageSource" value="{Binding ImageSource}" />

After that in your can set your image:
C#
ImageSource = new BitmapImage(new Uri("..\Images\Deleteicon.png", UriKind.Relative));


Edit
Or you can try something like this :
C#
<DataGrid ItemsSource="{Binding Customers}" AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Image" Width="SizeToCells" IsReadOnly="True">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button ImageSource="{Binding Image}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>


Edit2
In general you should create a style in xaml, with target type button. And then you could find it by code with FindResource() method, after you get the style, you could appily to your datagrid dynamically.
Or you can create style in code behind like here
 
Share this answer
 
v3
Comments
jing567 24-Oct-13 23:26pm    
Hello Alexander,
Code is very good.... please let me know that how to add image to a button created dynamically and the button is attatched to the datagrid through FrameworkElementFactory and cell template... But right now i cant use your code as i am not creating through xaml... pls provide the code in c#
Alexander Dymshyts 25-Oct-13 2:40am    
I don't really understand what should I provide... If I understood you right, you should create a property
public ImageSource Image
{
get
{
return _image;
}
set
{
if (!Equals(_image, value))
{
_image = value;
RaisePropertyChanged("Image");
}
}
}
and it's value will be applied to button. If you are using for example Actipro, then you do not need anything at all, they did it for us. Hope this helps
jing567 28-Oct-13 0:07am    
Hello Alexander...
Can you provide me the solution that how to add the image to that button... The code which you have provided is not so clear... inorder to implement in my solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900