Flot2011
Flot2011

Reputation: 4671

DataBinding doesn't work inside DataTemplate for DataGrid rowdetails

I have a WPF toolkit grid with rowdetails property turned on.

My rowdetails template is as follows

    <DataTemplate x:Key="rowDetailsTemplate">
    <Border BorderBrush="Black" BorderThickness="1" Background="#BFEFF2F5">
        <StackPanel Orientation="Horizontal">
                <StackPanel Orientation="Vertical">
                <ListBox Margin="100, 0, 0, 0"  ItemsSource ="{Binding Path=GridSubItems}">
                    </ListBox>
                </StackPanel>
        </StackPanel>
    </Border>
</DataTemplate>

Where GridSubItems defined in code behind:

     public class GridDataSource
{
    private List<GridItemData> _GridItems = new List<GridItemData>();
    private List<GridItemData> _GridSubItems = new List<GridItemData>();
    public List<GridItemData> GridItems
    {
        get
        {
            return _GridItems;
        }
        set
        {
            _GridItems = value;
        }
    }
    public List<GridItemData> GridSubItems
    {
        get
        {
            return _GridSubItems;
        }
        set
        {
            _GridSubItems = value;
        }
    }
}

and its data defined in XAML:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:myapp="clr-namespace:MyControlsDemo.DemoResources"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<myapp:GridDataSource x:Key="dgs">
    <myapp:GridDataSource.GridItems>
        <myapp:GridItemData Name="Item1" Type="Numeric" Unit="MHz" Key="i1" />
        <myapp:GridItemData Name="Item2" Type="List" Unit="enum" Key="i2" />
        <myapp:GridItemData Name="Item3" Type="Text" Unit="text" Key="i3" />
    </myapp:GridDataSource.GridItems>
    <myapp:GridDataSource.GridSubItems>
        <myapp:GridItemData Name="SubItem1" Type="Numeric" Unit="MHz" Key="si1" />
        <myapp:GridItemData Name="SubItem2" Type="Numeric" Unit="MHz" Key="si2" />
        <myapp:GridItemData Name="SubItem3" Type="Numeric" Unit="MHz" Key="si3" />
    </myapp:GridDataSource.GridSubItems>
</myapp:GridDataSource>

And that is how this datatemplate is used:

<ContentControl Grid.Column="2">
                <centerpanel:GenericParametersGrid DataContext="{StaticResource dgs}" 
                                       ItemsSource="{Binding Path=GridItems}"
                                       RowDetailsTemplate="{StaticResource ResourceKey=rowDetailsTemplate}"
                                       Style="{StaticResource ResourceKey=centerPanelHierParameterGridStyle}"/>                    
            </ContentControl>

However I cannot see subitems in my application, the listbox is shown empty. If I define subitems directly inside datatemplate, I can see them, so I guess the problem is in my data binding, but for the life of me I cannot see what is wrong. I should admit I have a very short experience with WPF.

Thanks a lot for any advice. flot

Upvotes: 0

Views: 911

Answers (1)

blindmeis
blindmeis

Reputation: 22435

GridSubItems has to be a Property of the ItemsSource for your DataGrid binding. otherwise you have to go with RelativeSource in your RowDetailsDataTemplate. but then its not real a child relation...

<DataGrid ItemsSource="{Binding MyDataWithAlsoAPropertyGridSubItems}">
  //Columns...
  //RowDetailsTemplate with Binding to GridSubItems 
</DataGrid>

EDIT: in your case your GridItemData object need a public property GridSubItems to work with RowDetailsTemplate straightforward

Upvotes: 2

Related Questions