Hi,
My situation is that for displaying fields in XamDataGrid, I have AutoGenerateFields="True" and then I collapse the fields in FieldLayoutInitialized event which I don't need. And then also in FieldLayoutInitialized event I have also set CellVisibilityWhenGrouped = Visibility.Collapsed with one of the field (lets call this field A ). I am also displaying another field (lets call this field B in the same column as field A but in 2nd row having field A in first row.)
Now what I want to do is since field A's CellVisibilityWhenGrouped = Visibility.Collapsed, its collapsed when grouped but field B in same column still remains visible. How can I make field B collapsed as well when field A is collapsed when its grouped.
I tried binding field B's Visibility with field A's Visibility but it didn't work.
Binding bin = new Binding(); bin.Source = layout.Fields["A"]; bin.Path = new PropertyPath("Visibility"); BindingOperations.SetBinding(layout.Fields["B"], Field.VisibilityProperty, bin);
Your help will be much appreciated.
Thanks,
Imad.
Anyone ?
Hello,
Thank you for your post. I have been looking into it and since the Visibility Property doesn’t change, if the Field is hidden only, when grouped, and the VisibilityResolved Property, which changes, is read only, I suggest you add the following code in your XamDataGrid’s Grouped event:
e.FieldLayout.Fields["B"].Visibility = e.FieldLayout.Fields["A"].VisibilityResolved;
Please let me know if this helps you or you need further assistance on this matter.
Looking forward for your reply.
I tried this. It works but it doesn't seem the prettiest way to handle this. As it would execute each time when any column is grouped.
I have been looking into your requirement and I suggest you add the following check and the code will execute only when you group Field “A”:
if(e.Groups.LastOrDefault().FieldName == "A") { e.FieldLayout.Fields["B"].Visibility = e.FieldLayout.Fields["A"].VisibilityResolved; }
Hope this helps you.
This solution doesn't work as I need to set the visibility of field B again when field A ungrouped and becomes visible. So it won't work if field A is not last when field A is ungrouped. Infact when we ungroup field A, the e.Group collection no longer contains field A.
Hello Imad,
In that case you can use the following code in order to have everything works as you want:
bool isInGroup = false; foreach (var item in e.Groups) { if (item.FieldName == "A") { isInGroup = true; } } if (isInGroup) e.FieldLayout.Fields["B"].Visibility = System.Windows.Visibility.Collapsed; else e.FieldLayout.Fields["B"].Visibility = System.Windows.Visibility.Visible;
Thanks for the solution, I was also planning to do something similar.
Thank you for your feedback. I am glad that you resolved your issue and I believe that other community members may benefit from this as well.
Thanks again.