从 XAML 中的内部列表视图访问外部列表视图项目源 属性?

Access an outer listview item source property from an inner listview in XAML?

这里我将一个列表嵌套到另一个列表中。 a属性是表单项源outerItemSource。所以现在我想从内部列表访问这个 属性 。我如何实现它?这是我的代码

<ListView ItemsSource="{Binding outerItemSource}"  >
<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <TextBlock Text="{Binding a}" Width="50"></TextBlock>
            <TextBlock Text="{Binding b}" Width="50" Grid.Row="1"></TextBlock>
            <TextBlock Text="{Binding c}" Width="50" Grid.Row="2"></TextBlock>
            <TextBlock Text="{Binding d}" Width="50" Grid.Row="3"></TextBlock>

            <ListView ItemsSource="{Binding innerItemSource}" Grid.Row="4">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <--a is from outerItemSource-->
                            <TextBlock Text="{Binding a}" />

                            <TextBlock Text="{Binding f}" />
                            <TextBlock Text="{Binding g}" />
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

这既可以使用代码完成,也可以仅 XAML 完成。

第一种方式

public partial class MainWindow : Window
{
    public ObservableCollection<outerItemSource_class> outerItemSource { get; set; }
    public ObservableCollection<innerItemSource_class> innerItemSource { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        outerItemSource = new ObservableCollection<outerItemSource_class>();
        outerItemSource.Add(new outerItemSource_class());
        outerItemSource.Add(new outerItemSource_class());
        DataContext = this;
    }
    
}
public class outerItemSource_class
{
    public string a { get; set; }
    public string b { get; set; }
    public string c { get; set; }
    public string d { get; set; }
    public ObservableCollection<innerItemSource_class> innerItemSource { get; set; }
    public outerItemSource_class()
    {
        a = "outer a";
        b = "outer b";
        c = "outer c";
        d = "outer d";
        innerItemSource = new ObservableCollection<innerItemSource_class>();
        innerItemSource.Add(new innerItemSource_class(a));
    }
}
public class innerItemSource_class 
{
    public string outer_a { get; set; }
    public string e { get; set; }
    public string f { get; set; }
    public string g { get; set; }
    public innerItemSource_class(string a)
    { 
        outer_a = a;
        e = "inner e";
        f = "inner f";
        g = "inner g";
    }
}

XAML:

    <ListView ItemsSource="{Binding outerItemSource}"  >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="{Binding a}" Width="50"></TextBlock>
                    <TextBlock Text="{Binding b}" Width="50" Grid.Row="1"></TextBlock>
                    <TextBlock Text="{Binding c}" Width="50" Grid.Row="2"></TextBlock>
                    <TextBlock Text="{Binding d}" Width="50" Grid.Row="3"></TextBlock>

                    <ListView ItemsSource="{Binding innerItemSource}" Grid.Row="4">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding outer_a}" />

                                    <TextBlock Text="{Binding f}" />
                                    <TextBlock Text="{Binding g}" />
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListView>

第二种方式

    <ListView x:Name="OuterListView" ItemsSource="{Binding outerItemSource}"  >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <TextBlock x:Name="TextBlock_a" Text="{Binding a}" Width="50"></TextBlock>
                    <TextBlock Text="{Binding b}" Width="50" Grid.Row="1"></TextBlock>
                    <TextBlock Text="{Binding c}" Width="50" Grid.Row="2"></TextBlock>
                    <TextBlock Text="{Binding d}" Width="50" Grid.Row="3"></TextBlock>

                    <ListView ItemsSource="{Binding innerItemSource}" Grid.Row="4">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>                                        
                                    <TextBlock Text="{Binding ElementName=TextBlock_a, Path=Text, 
                                        UpdateSourceTrigger=PropertyChanged}" />

                                    <TextBlock Text="{Binding f}" />
                                    <TextBlock Text="{Binding g}" />
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListView>

不管怎样,结果都是一样的