Answered by:
Binding Command to Button within DataTemplate

Question
-
Hi,
I am using WPF Toolkit in my project. And I have DataGrid in my XAML.
Now I have added one button in one column using DataTemplate.I have binded DataView to this Grid. But the only problem am facing is am not able to bind Command to the Button within datatemplate.
I have written following XAML to bind command to this button
<Button Content="..." Command = "{Binding Path=ShowDealListCommand}"/>
But when I clicked this button it's not calling target method I have written in this command.
I am able to bind content property to this button so this means that there is no issues of setting datacontext. As I have already set DataContext for this XAML in Codebehind.
Pls help me !!
Thanks ! :)
Friday, November 19, 2010 7:33 AM
Answers
-
Hi SuhaX
Welcome to our forum.
I am not sure the root cause of your issue without reproducing your problem, so I can give you some solutions you can have a try.
1) You can try to define your binding like this:
Command="{Binding ElementName=" YourControl.Name ", Path=DataContext.PerformActionCommand}"
2) if it cannot work on your application, I think maybe other controls have handled your click event, so you can not call your command action, you can try to change your command binding to Click event, check this click event whether works on your application.
If the Click event does not work, then you can know your cause of this issue is not command binding, then you can check which control handles your click event.
On the other hand, you can know your cause of this issue is command binding, you can try to refer Kevin's reply from this link:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b6146e3e-7bfa-4c0b-a2ca-6a7f04d0cf47
If your issue persists, could you share more information with me to reproduce your issue, I will do my best to resolve your issue as soon as possible.
Best regards,
Sheldon _Xiao
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!- Marked as answer by Sheldon _Xiao Thursday, November 25, 2010 10:43 AM
Wednesday, November 24, 2010 8:58 AM
All replies
-
Hi,
You would have to refer from the parent items datacontext.As each of the datagridrow's data context get set to individual item of your itemssource.
Specify RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ItemsControl}}
Check this link
Feel free to ask if you need more help
Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problemFriday, November 19, 2010 7:53 AM -
Hi Rahul,
Thanks for the reply.
but I have already tried this option its not workin :(
Any otehr ways?
Friday, November 19, 2010 11:12 AM -
Hi,
Could you you please put up the code that you are trying.
Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problemFriday, November 19, 2010 11:44 AM -
Hi,
Am following MVVM Pattern in my project. Now am having DataGrid in my XAML :
XAML:
. . .
<toolkit:DataGrid Name="temp" Height="500" Grid.Row="1" VerticalAlignment="Top" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True" ItemSource="myDataView" />
<toolkit:Column.../>
<toolkit:Column.../>
<toolkit:Column.../><toolkit:Column...>
<DataTemplate><Button Command="ShowCommand">Show</Button>
</DataTemplate>
</toolkit:Column>
</toolkit:DataGrid>I have set datacontext for this XAML in Code Behind.
CODE BEHIND:
this.DataContext = new ModelClass();
And in ModelClass i have written a SimpleCommand. And the same Command I have bind on XAML. But when I clicked on button its not calling the action method mentioned in this simple Command...SimpleCommand showCmd = new SimpleCommand<string> (this.ShowMethod, this.CanShow);
Please Help me ! How Can I bind the ShowCommand to Button within DataTemplate !!
THANKS !!
Sunday, November 21, 2010 8:00 AM -
Hi SuhaX
Welcome to our forum.
I am not sure the root cause of your issue without reproducing your problem, so I can give you some solutions you can have a try.
1) You can try to define your binding like this:
Command="{Binding ElementName=" YourControl.Name ", Path=DataContext.PerformActionCommand}"
2) if it cannot work on your application, I think maybe other controls have handled your click event, so you can not call your command action, you can try to change your command binding to Click event, check this click event whether works on your application.
If the Click event does not work, then you can know your cause of this issue is not command binding, then you can check which control handles your click event.
On the other hand, you can know your cause of this issue is command binding, you can try to refer Kevin's reply from this link:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b6146e3e-7bfa-4c0b-a2ca-6a7f04d0cf47
If your issue persists, could you share more information with me to reproduce your issue, I will do my best to resolve your issue as soon as possible.
Best regards,
Sheldon _Xiao
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!- Marked as answer by Sheldon _Xiao Thursday, November 25, 2010 10:43 AM
Wednesday, November 24, 2010 8:58 AM -
<UserControl x:Class="ShipmentView.Templates.SchedBBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <TextBox Text="{Binding ItemIntrastat.SchedBNumber}" MaxLength="31" Grid.Column="0"/> <Button Content="..." Grid.Column="1" Name="btn_OpenTccPicker" Command="{Binding Path=TccOpener}" /> </Grid> </UserControl>
I don't have an answer. I have an identical problem. My WPF main window has three buttons that are bound to commands that work fine. I created a datatemplate for the cell editing template within a datagrid and the button in the datatemplate is non-responsive. This is strange because the textbox inside the datatemplate still shows the expected text inside of it suggesting that the datatemplate is still respecting the datacontext, but for some reason decides that the command doesn't need to be acted upon.
Anyone out there with an answer to this?
Tuesday, September 11, 2012 6:19 PM -
I just found this on stack overflow and it worked for me.
http://stackoverflow.com/questions/581715/binding-to-a-command-in-a-datagrid
Specifically, this is what I used based on the previous forum link.
<UserControl x:Class="ShipmentView.Templates.SchedBBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <TextBox Text="{Binding ItemIntrastat.SchedBNumber}" MaxLength="31" Grid.Column="0"/> <Button Content="..." Grid.Column="1" Name="btn_OpenTccPicker" Command="{Binding Path=DataContext.TccOpener, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}}}" /> </Grid> </UserControl>
- Proposed as answer by MrGoodfix Tuesday, September 11, 2012 6:49 PM
Tuesday, September 11, 2012 6:48 PM -
Thank you MrGoodfix! This helped me figure it out.Tuesday, January 21, 2014 7:48 PM