I have a grid that contains a sublist under it. I would like to display data on that parent record row only if the row is not expanded. For example, I have a Toy and a Child object as defined below. I only want to show the ParentName if the row is collapsed. Is this possible?
public class Toy
{
public string ID { get; set; }
public string Name { get; set; }
}
public class Child
public string ParentName { get; set; }
public List<Toy> Toys { get; set; }
I was able to get it partially there. I created a style on the CellValuePresenter and assigned it to the ParentName. I added to the Child class a property IsExpanded and made it implement INotifyPropertyChanged. Furthermore, I change the property when the row is expanded or collapsed and that hides the ParentName column. However, there's a very annoying bug that I'm having trouble deciphering. When I hover over the Name column when the row's are expanded, the ParentName is displayed. I'm not sure why.
I've attached the source code.
Kyle,
It is easier to accomplish hiding of the value by retemplating the CellValuePresenter to contain a TextBlock and binding it's Visibility to the IsExpanded property:
<BooleanToVisibilityConverter x:Key="booltovis"/> <Style TargetType="{x:Type igDP:CellValuePresenter}" x:Key="MyStyle"> <Setter Property="Foreground" Value="Black" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <TextBlock Margin="2" Text="{TemplateBinding Value}" Visibility="{Binding Path=DataItem.IsExpanded, Converter={StaticResource booltovis}}"/> </ControlTemplate> </Setter.Value> </Setter> </Style>
Note that the above example is backwards in that the TextBlock will be visible at the wrong time if added to the code that you provided. You can correct that either by using a different converter or exposing a property that determines if the value should be visible rather than showing the expanded state.
Let me know if you have any questions with this matter.
This worked fine until I am supposed to make the row have a non-white background color. As soon as I hide the cell, the background color returns to white. Is there a way around this so I can have a background color of green with nothing in it depending on whether row is expanded or collapsed?
Hello,
Are you able to post the XAML for your CellValuePresenterStyle as well as how you are setting the background of the row?