Quantcast
Channel: Is it possible to parameterize the binding on a DataGrid CellTemplate in a resource? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Is it possible to parameterize the binding on a DataGrid CellTemplate in a resource?

$
0
0

I'm putting together some generic CellTemplate styles for a WPF grid (WPFToolKit DataGrid), and I’m not sure of the syntax to use to make the binding generic. So for example, I have this template, which turns the value red if the value is negative:

<DataTemplate x:Key="RedNegativeCellTemplate"><TextBlock Name="QuantityTextBox" Text="{Binding Quantity, StringFormat='c', Mode=OneWay}"/><DataTemplate.Triggers><DataTrigger Binding="{Binding Quantity, Converter={StaticResource SignConverter}}" Value="-1"><Setter TargetName="QuantityTextBox" Property="Foreground" Value="Red"/></DataTrigger></DataTemplate.Triggers></DataTemplate>

You’ll notice that this has a binding on the Text box to the column value ‘Quantity’ - i.e., the field/column that the binding comes from is explicit.

So I can use this in my WPF DataGrid as follows:

<sdk:DataGrid ItemsSource="{Binding MyDataSource}" AutoGenerateColumns="False"><sdk:DataGrid.Columns><sdk:DataGridTemplateColumn Header="Net Quantity" CellTemplate="{StaticResource RedNegativeCellTemplate}"/></sdk:DataGrid.Columns></sdk:DataGrid>

But… what I’d really like to do is to have the field binding generic for the template, so that I can re-use the template as follows:

<sdk:DataGrid ItemsSource="{Binding OrdersQuery}"AutoGenerateColumns="False"><sdk:DataGrid.Columns><sdk:DataGridTemplateColumn Header="Quantity" CellTemplate="{StaticResource RedNegativeCellTemplate}"/><sdk:DataGridTemplateColumn Header="Price" CellTemplate="{StaticResource RedNegativeCellTemplate}"/><sdk:DataGridTemplateColumn Header="Total" CellTemplate="{StaticResource RedNegativeCellTemplate}"/></sdk:DataGrid.Columns></sdk:DataGrid>

Now, the problem here is that there doesn't seem to be a way to parameterize the binding in the CellTemplate. But I'm loath to have to have 3+ lines of controltemplate for every column that uses this template, as it clutters the XAML and makes it far less readable (not to mention the fact that if I decide to change the cell template to put a border around the textbox, I'd have to modify it in multiple places.

So I'm thinking that the binding within the CellTemplate should look something like this (note that we're using '.' for the binding path):

            Text="{Binding Path=., StringFormat='c', Mode=OneWay}"

And then somehow set the datacontext from the DataGridTemplateColumn declaration – but I can’t see how to do it.

Any idea how to do this?


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles



Latest Images