1

I have a ListView with an nested ItemTemplate for presenting orders. Each order is presented within a Expander. These Expanders have a ContentTemplate for presenting all positions within each order. And these order positions are also in a Expander. The ListView gets its data from an ObservableCollection (AvailableOrders) which contains all orders. These order object have a ObservableCollection "Items" holding all positions for this order. But I'm not able to get the bindings work properly. How should I properly set the binding for the "inner expander" to show information about the items?

All ideas are appreciated!

<ListView ItemsSource="{Binding VMOrder.AvailableOrders}">
<ListView.ItemTemplate>
    <DataTemplate>
        <Expander Content="{Binding}">
            <Expander.HeaderTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Order " />
                        <TextBlock Text="{Binding Id}" />
                    </StackPanel>
                </DataTemplate>
            </Expander.HeaderTemplate>
            <Expander.ContentTemplate>
                <DataTemplate>
                    <ItemsControl ItemsSource="{Binding Items}">
                        <ItemsControl.Template>
                            <ControlTemplate>
                                <Expander>
                                    <Expander.HeaderTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Material.Name}" />
                                        </DataTemplate>
                                    </Expander.HeaderTemplate>
                                    <Expander.ContentTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="TEST" />
                                        </DataTemplate>
                                    </Expander.ContentTemplate>
                                </Expander>
                            </ControlTemplate>
                        </ItemsControl.Template>
                    </ItemsControl>
                </DataTemplate>
            </Expander.ContentTemplate>
        </Expander>
    </DataTemplate>
</ListView.ItemTemplate>
</ListView>
4

2 回答 2

3

I've figured it out now. I need to use a relative source in the data templates and set the content property of each expander.

<ListView ItemsSource="{Binding VMOrder.AvailableOrders}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Expander Content="{Binding}">
                <Expander.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Order " />
                            <TextBlock Text="{Binding DataContext.Id, RelativeSource={RelativeSource FindAncestor, AncestorType=Expander}}" />
                        </StackPanel>
                    </DataTemplate>
                </Expander.HeaderTemplate>
                <Expander.ContentTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding Items}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Expander Content="{Binding}">
                                        <Expander.HeaderTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding DataContext.Material.Name, RelativeSource={RelativeSource FindAncestor, AncestorType=Expander}}" />
                                            </DataTemplate>
                                        </Expander.HeaderTemplate>
                                        <Expander.ContentTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding DataContext.Material.Description, RelativeSource={RelativeSource FindAncestor, AncestorType=Expander}}" />
                                            </DataTemplate>
                                        </Expander.ContentTemplate>
                                    </Expander>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </DataTemplate>
                </Expander.ContentTemplate>
            </Expander>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
于 2013-10-05T00:46:02.343 回答
1

对于内部 ItemsControl,您已经为整个控件定义了控件模板。您必须改为定义 ItemTemplate

  <ItemsControl ItemsSource="{Binding Items}">
      <ItemsControl.ItemTemplate>
          <DataTemplate>
              <Expander>
                  <Expander.HeaderTemplate>
                      <DataTemplate>
                          <TextBlock Text="{Binding Material.Name}" />
                      </DataTemplate>
                  </Expander.HeaderTemplate>
                  <Expander.ContentTemplate>
                      <DataTemplate>
                          <TextBlock Text="TEST" />
                      </DataTemplate>
                  </Expander.ContentTemplate>
              </Expander>
          </DataTemplate>
      </ItemsControl.ItemTemplate>
  </ItemsControl>
于 2013-10-04T02:30:57.807 回答