ListView数据绑定内部ListView上的XAML绑定行为

tzdcorbm  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(179)

我有课

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

正在尝试将ObservableCollection的数据绑定到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>

但在绑定“操作无效,而项源正在使用访问”时出现错误
当我从list2中删除DataTemplate时,它正确地显示了数据绑定,带有名称和相应的标记的listview。

<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>

我正在将数据源指定为

list1.ItemsSource = observablecollection<Test>;

我尝试添加给定路径,相对源,但仍然面临错误。我不明白为什么把文本块导致错误?

uxh89sit

uxh89sit1#

DataTemplate周围缺少<ListView.ItemTemplate>标记:

<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>

加价

<ListView>
    <DataTemplate>

    </DataTemplate>
</ListView>

相当于

<ListView>
  <ListView.Items>
    <DataTemplate>

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

和项用法与设置ItemsSource冲突。
<ListView.Items>可以省略,因为Items属性被选择为ListView ContentProperty

相关问题