Greetings!
I'm having a problem trying to bind a heirarchical column style to its parent row's DataContext in xaml.
My grid's datasource is a collection of ViewModels, each of which has a property MyProperty. I need to set a style on a border that is in a datatemplate inside of a TemplatedColumn in a ColumnLayout inside of the grid, according to that parent ViewModel.MyProperty value (with a converter of course). That's a lot of nesting!
Here is a sketch of my problem:
<ig:XamGrid ItemsSource="{Binding Path=ItemViewModels}">
...
<ig:ColumnLayout> <<<<<<child level in heirarchy
<ig:TemplateColumn Key="SomeColumn">
<ig:TemplateColumn.ItemTemplate>
<DataTemplate>
<Border BorderBrush="{Binding RowData Path=MyProperty, Converter={StaticResource MyPropertyToBrushConverter}}"> //This does not work obviously, since I'm posting!
I'd appreciate any help you can give!
And hello again Martin!
We're almost there, I can feel it!...but.. ;)
The good news is, yes, that is exactly the kind of thing I've been trying to construct...the bad news is that I cannot seem to get it working myself. For one thing, I am using Silverlight (as noted in the question title) and Silverlight does not use the x:Type markup.
Things I have done (and failed to succed with):
As I said, we're getting close! But I'm feeling maybe it is going to take an example on your end (Silverlight) to actually show me that it really is possible. Otherwise, I'm going to have to scrap this approach, which would really suck. :(
So, thanks for your help so far, and I'm looking forward to the resolution!
Hello again William,
I understand what you are trying to achieve now. There is a solution for that, too. So, what you need to do is just change the BorderBrush's binding. Here is an example of what you need to type:
<Border BorderBrush="{Binding RelativeSource={RelativeSource AncestorType={x:Type prim:CellsPanel}, AncestorLevel=2}, Path=Row.Data.ContactName}"/>
,where prim will be the http://schemas.infragistics.com/xaml/primitives/ namespace, defined in the beginning of the XAML file. The Row.Data.ContactName is the path to the property of the parent row, which value you need to bind to. The only thing you need to add is your converter at the end. So the final syntax could look something like this:
<Border BorderBrush="{Binding RelativeSource={RelativeSource AncestorType={x:Type prim:CellsPanel}, AncestorLevel=2}, Path=Row.Data.ContactName, Converter=MyConverter}"/>
Of course you just need to change the "ContactName" to your specific property.
Hope this will help you,
Martin Stoev
Hi Martin! Thanks for the quick reply.
Sadly, however, that does not quite meet my requirement. :X
It does however give me another way of describing my question. In that example that you pointed me to, there is a property called "Orders". That properties is a nested collection represented by another embedded "child" xamGrid. This grid exists for each individual RowData in the parent grid (Name="dataGrid"). This child xamGrid has the following column declared:
<ig:TextColumn Key="OrderDate" FormatString="{}{0:d}"> <ig:TextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text="{Binding Path=XWG_Orders_OrderDateLabel, Source={StaticResource Strings}}" /> </DataTemplate> </ig:TextColumn.HeaderTemplate></ig:TextColumn>
My requirement is specifically getting a reference to the RowData of the parent xamGrid's(Name="dataGrid"). It would look something similar to the following:
<ig:TextColumn Key="OrderDate" FormatString="{}{0:d}"> <ig:TextColumn.HeaderTemplate> <DataTemplate> <Border BorderBrush="{Binding [MarkupWouldPointTo"dataGrid" RowData], Converter=MyConverter}"> <TextBlock Text="{Binding Path=XWG_Orders_OrderDateLabel, Source={StaticResource Strings}}" /> </Border> </DataTemplate> </ig:TextColumn.HeaderTemplate></ig:TextColumn>
Essentially, I am trying to change the border color in my data template based on data in the parent grid's RowData. In the above example, the analog would be trying to get a border around the OrderDate field, based on the parent grid's (Name="dataGrid") ContactName's value.
Does that help explain my question? As far as I can tell though, changing to TemplateColumnLayout does not address this specific issue. If it is not possible, then no big sweat I'll figure something else out. But it would seem to me that it should be possible to get a hold of the parent grid's row data through xaml.
Hello William,
Thank you for your post. There is a quick solution for your issue - change the <ig:ColumnLayout> to <ig:TemplateColumnLayout> (since you are using <ig:TemplateColumn> inside). Also don't forget to set the "Key" property on the ig:TemplateColumnLayout. After doing that, all of the bindings should work fine.
You can refer to this sample for a quick demo on how to set it all up.
If that happens not to be your case, then you could share a samples and we'll be happy to help.
Thanks,