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!
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,
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 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,
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!
Hold off on that though...I had tried going back to the ColumnLayout from the TemplateColumnLayout approach. Let me try it first with the TemplateColumnLayout and embedded XamGrid and see if that works first...Thank You!
No, I'm afraid I can't convert it to a TemplateColumnLayout. No matter what I do, I get the following error:
Error: Unhandled Error in Silverlight Application Code: 4004
Category: ManagedRuntimeError
Message: System.InvalidOperationException: MeasureOverride of element 'Infragistics.Controls.Grids.Primitives.ColumnLayoutTemplateRowCellsPanel' should not return PositiveInfinity or NaN as its DesiredSize. at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
I've tried removing all references to styles in my xaml (which I shouldn't have to do) and I still get the error. When I do a concept application, I do not get the error.
At this point, I'm about done with investing in this issue. I am simply going to mark it down as not feasible. Thanks for your time!
Hi Bill,
I'm happy to hear that this worked our for you. This was a really interesting case that you had. You also did a great job investigating it, and finally we both got to the result needed. Thanks for that. Now, other users may benefit from this as well. And if you have any other questions, I'll be happy to help again.
Thanks again,
Yes, that's exactly what I did. I tried it again, and I was still getting the error. Because I appreciated your good attitude and there was an example app, I went ahead and took another go at troubleshooting the error I was getting when using the TemplateColumnLayout.
As I stated before, I was getting the following error:
I thought these had something to do with my styling, but alas it was a bug in the version of Infragistics that I'm using (and I have no power to get our team to upgrade at this time :X). The bug is that if you do not explicitly set the height and width of the grid (i.e. if you let the grid stretch to its container), then it throws that error. Maybe this has been fixed in future versions, but in my version this bug exists.
So, I'm taking the minor hit of being forced to explicitly state the height and width in exchange for the cool (and really necessary) styling.
Thanks for your perseverance Martin. Much props to you and your team.
-Bill Raiford
I've found the solution for your issue in Silverlight. The right way was to create the custom x:Type markup, as you found in this post. You were really close to solving it. I think the best way for you to see it, is by downloading the sample I've attached. It's just a simple SL application where such functionality is achieved (even using a converter). Please, download it, examine it, and if you need some help or if you have any questions, you know we'll be glad to help. :)