views:

188

answers:

2

Hi, I'm trying to create fancy looking Listbox. ListBoxItems are supposed to expand after being selected, but the problem is, they're also supposed to contain another ListBox filled with some details about particular item and I have no idea how to put some data into it. I've tried both accessing it from C# code and binding it in XAML but I'm still nowhere near the solution.

<UserControl.Resources>        
    <ResourceDictionary>
    <DataTemplate x:Key="SelectedTemplate">
            <StackPanel Orientation="Vertical">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path = Order}" Style="{StaticResource SampleListCellItem}" MinWidth="35"/>
                    <TextBlock Text="{Binding Path = FullName}" Style="{StaticResource SampleListCellItem}" Width="340"/>                     
                    <TextBlock Text="{Binding Path = FirstName}" Style="{StaticResource SampleListCellItem}" Width="200" />
                    <TextBlock Text="{Binding Path = BirthDate, StringFormat = d}" Style="{StaticResource SampleListCellItem}" Width="100"/>
                </StackPanel>
                <StackPanel HorizontalAlignment="Right">
                    <ListBox Name="InnerList" Height="200" Width="200"/>
                    <Button Name="Button1" Height="40" Width="100" Content="ButtonText" Visibility="Visible"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="ItemTemplate">
            <StackPanel Orientation="Vertical">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path = Order}" Style="{StaticResource SampleListCellItem}" MinWidth="35"/>
                    <TextBlock Text="{Binding Path = FullName}" Style="{StaticResource SampleListCellItem}" Width="340"/>
                    <TextBlock Text="{Binding Path = FirstName}" Style="{StaticResource SampleListCellItem}" Width="200" />
                    <TextBlock Text="{Binding Path = BirthDate, StringFormat = d}" Style="{StaticResource SampleListCellItem}" Width="100"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>

        <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
            <Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</UserControl.Resources>
+1  A: 

It sounds like what you're looking for is a tree. I think the TreeView control would be ideal for what you're looking for.

apandit
Thank You both for an answer and sorry for the delay in responding. David's solution works like a charm. Thanks
A: 

In your item that you want to display, I assume you have a property that is a list (of T) or other type that is compatible with a normal listbox itemsource. Let name it ListPr for the sake of example.

In your data template, add a listBox control and set the ItemsSource to {Binding Path = ListPr }. It should works just like that. In that new listbox, you will be able to define another data template, and so on.

Like it's mentioned in another post though, a treeview might be what you need in this case.

Hope that helps.

David Brunelle