Hello
Has anybody got an example of a XamGrid that successfully uses commanding on a button within the item template of a template column?
I have tried Prism commands and Silverlight ICommands with no luck. The command fires OK in my viewmodel when I move the button outside of the XamGrid but not when it's in the column template. When i have done this before within in a listbox itemtemplate the addition of ElementName=LayoutRoot and adding DataContext to the binding path has worked but it doesn't seem to in this case.
Here's my XAML.
<ig:TemplateColumn Key="AliasId"
Width="Auto"
HorizontalContentAlignment="Center"
HeaderTextHorizontalAlignment="Center">
<ig:TemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding ItemAliasStrings.DeleteColumn, Source={StaticResource ItemAliasStrings}}"></TextBlock>
</DataTemplate>
</ig:TemplateColumn.HeaderTemplate>
<ig:TemplateColumn.ItemTemplate>
<Button cal:Click.Command="{Binding DataContext.DeleteCommand, ElementName=LayoutRoot}"
Content="{Binding ItemAliasStrings.DeleteColumn, Source={StaticResource ItemAliasStrings}}"
Margin="5,0,5,0" />
</ig:TemplateColumn.ItemTemplate>
</ig:TemplateColumn>
Thanks
Kevin
Hi Kevin,
You could create a proxy object, with a ViewModel property.
Then put the proxy object in your Resources collection
<UserControl.Resources> <local:ViewModelProxy x:key="vmp"/></UserControl.Resources>Then, when you've loaded:((ViewModelProxy)this.Resource["vmp"].ViewModel = viewModel;
Hope this helps,
-SteveZ
Thanks SteveZ
I've changed the command binding from elementname to source however my viewmodel is dependency injected into the view constructor using a unity container rather than being a staticresource. I've tried adding it as a resource in code in the view constructor i.e.
this.Resources.Add("viewModel", viewModel);
But the the output shows an exception finding the command property on the viewModel staticresource.
Any ideas?
Thanks again
Unfortunately, b/c we load the DataTemplates via their LoadContent method, the Templates don't support ElementName. Its basically a Silverlight limitation. So, to access your DataContext, you need to do so via a StaticResource.
Something like:
<Button cal:Click.Command="{Binding Path=DeleteCommand, Source={StaticResource YourDataContext}"/>