Home > Enterprise >  XAML binding behaviour on ListView inside ListView data binding
XAML binding behaviour on ListView inside ListView data binding

Time:07-15

I have a class

Class Test
{
  string name {get;set;}
  List<string> marks {get; set;}
}

Trying to bind data of ObservableCollection to XAML

<ListView x:Name="list1"  HorizontalAlignment="Left"  VerticalAlignment="Top" ItemsSource="{Binding}" Width="790">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment"
            Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>

        <ListView.View>
            <GridView >
                <GridViewColumn Header="Name"   HeaderTemplate="{StaticResource myHeaderTemplate}"  HeaderContainerStyle="{StaticResource myHeaderStyle}" Width="150">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock  x:Name="xyt" Text="{Binding name}" TextAlignment="Center" FontFamily="Arial" FontSize="14">
                            </TextBlock>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
               
                <GridViewColumn Header="Marks"   HeaderTemplate="{StaticResource myHeaderTemplate}"  HeaderContainerStyle="{StaticResource myHeaderStyle}" Width="500">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            **<ListView x:Name="list2" ItemsSource="{Binding marks}">
                                <DataTemplate>
                                   
                                    <TextBlock  x:Name="marki" Text="{Binding}" TextAlignment="Center" FontFamily="Arial" FontSize="16" Margin="10" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown">
                                    </TextBlock>
                                   
                                </DataTemplate>
                            </ListView>**
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

            </GridView>
        </ListView.View>
    </ListView>

But I get error in binding "operation-is-not-valid-while-itemssource-is-in-use-access"

When I remove DataTemplate from list2, it shows properly with data binding, with name and corresponding listview of marks.

  <ListView x:Name="list1"  HorizontalAlignment="Left"  VerticalAlignment="Top" ItemsSource="{Binding}" Width="790">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment"
            Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>

        <ListView.View>
            <GridView >
                <GridViewColumn Header="Name"   HeaderTemplate="{StaticResource myHeaderTemplate}"  HeaderContainerStyle="{StaticResource myHeaderStyle}" Width="150">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock  x:Name="xyt" Text="{Binding name}" TextAlignment="Center" FontFamily="Arial" FontSize="14">
                            </TextBlock>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
               
                <GridViewColumn Header="Marks"   HeaderTemplate="{StaticResource myHeaderTemplate}"  HeaderContainerStyle="{StaticResource myHeaderStyle}" Width="500">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            **<ListView x:Name="list2" ItemsSource="{Binding marks}">
                                
                            </ListView>**
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

            </GridView>
        </ListView.View>
    </ListView>

I am assigning datasource as

list1.ItemsSource = observablecollection<Test>;

I tried adding giving Path, relativesource, but still facing error. I am confused why putting TextBlock causing error?

CodePudding user response:

you are missing <ListView.ItemTemplate> tag around DataTemplate:

<ListView x:Name="list2" ItemsSource="{Binding marks}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <TextBlock  x:Name="marki" Text="{Binding}" TextAlignment="Center" FontFamily="Arial" FontSize="16" Margin="10" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"/>
    </DataTemplate>
  <ListView.ItemTemplate>
</ListView>

markup

<ListView>
    <DataTemplate>

    </DataTemplate>
</ListView>

is equivalent to

<ListView>
  <ListView.Items>
    <DataTemplate>

    </DataTemplate>
  <ListView.Items>
</ListView>

and Items usage conflicts with setting ItemsSource.

<ListView.Items> can be omitted because Items property is chosen as ContentProperty for ListView.

  • Related