This project is read-only.

DataGrid TemplatedColumn with button doesn't pass a DependencyParameter

Jan 20, 2011 at 10:14 AM

Hi,

I have a standard data grid form the SL Toolkit. It has two columns and one is templated. The template looks like this:

<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel
Orientation="Horizontal"
HorizontalAlignment="Stretch"
Margin="4">
<Button
Content="Test">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<n:ExecuteCommandAction Command="{Binding EdycjaWarunku,
Source={StaticResource ViewModel}}">
<n:DependencyParameter
Value="{Binding ElementName=warunkiPromocjiGrid, Path=SelectedItem}"/>
</n:ExecuteCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>

The method in my ViewModel executes fine but no Dependency Parameter is passed. When I take the Button code and place it outside the grid column it works fine. Any suggestions on that?

 

Jan 20, 2011 at 1:50 PM

The problem is same is I mentioned earlier, you can't access an instantiated member (in the Visual-Tree) from the DataTemplate directly - think of a DataTemplate as being a type definition (ie. var _x = typeof(Foo)), and a type definition cannot access an instantiated instance (i.e. var _y = new Foo()) directly. This is the reason we have value relays - they act like static members  (e.g. var _z = Foo.Message), which you can access without instantiating an instance using StaticResource binding.

Also FYI, in most cases you can bind directly to the CommandParameter without having to use a DependencyParameter, eg:

<i:EventTrigger EventName="Click">
<
n:ExecuteCommandAction Command="{Binding EdycjaWarunku, Source={StaticResource ViewModel}}" Parameter="{Binding Value}" />
</i:EventTrigger>

Hope this helps,
Rishi 

Jan 20, 2011 at 2:13 PM
Edited Jan 20, 2011 at 2:27 PM

Ok, so what would you suggest to do? Becuase I can't find any example with value relay for nRoute.

Jan 20, 2011 at 11:47 PM

Value Relay Info: http://www.orktane.com/Blog/post/2009/10/10/Introducing-nRouteToolkit-for-Silverlight-(Part-II).aspx

Value Relay Sample: http://www.orktane.com/Blog/post/2010/07/12/Creating-a-Netflix-App-using-nRoute-again-Now-in-WPF.aspx (more in repository)

Cheers,
Rishi

Jan 21, 2011 at 8:39 AM

Hmm... I've declared the ValueRelay:

<n:ValueRelay x:Key="WarunekPromocjiValueRelay"/>

Also the ExecuteCommand:

<n:ExecuteCommandAction Command="{Binding EdycjaWarunku, Source={StaticResource ViewModel}}" Parameter="{Binding Value, Source={StaticResource WarunekPromocjiValueRelay}, Mode=TwoWay}"/>

But I don't get the code behind part of it. To what should I hook up? The the buttons load event and there set the value for the parameter?

Jan 21, 2011 at 1:28 PM

I did something like this:

private void Button_Loaded(object sender, RoutedEventArgs e)
{
this.SetRelayValue<WebApplication2.WarunkiPromocji>("WarunekPromocjiValueRelay",
(sender as Button).DataContext as WebApplication2.WarunkiPromocji);
}

But the problem is that all the buttons have the same datacontext. Every button should have the context of the it's placed in. Any suggestions?

Jan 31, 2011 at 11:50 AM

Here, I've created a small sample showing how to use a command from a cell-template:

http://cid-587cbdf035b4a11d.office.live.com/self.aspx/.Public/DataTemplateCommandsSample.zip

Hope this helps,
Rishi