如何为控件的 属性(在 DataTemplate 和 UserControl 中)设置绑定以使用 ItemSource 的给定 属性?

How can I set the binding for a control's property (which is inside DataTemplate and UserControl) to use the ItemSource's given property?

我想制作一个具有 DataTemplate 的 UserControl,并且在该 DataTemplate 中有控件。我想绑定到那些嵌套(在 DataTemplate 内)控件的属性,以便在我重用此 UserControl 时设置它们。嵌套控件将使用 ItemSource 的属性,但 属性 ItemSource 属性的名称可能不同。

用户控件:

<UserControl x:Class="ContextMenu.BaseFilterUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             x:Name="Self">
    <Grid Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="80" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="70" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Column="0"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Right"
                   Margin="10"
                   Text="Owners" />
        <Button Grid.Column="1"
                VerticalAlignment="Center"
                HorizontalAlignment="Center"
                Margin="10"
                Click="FilteButtonClicked"
                Width="40"
                Height="40"
                x:Name="FilterButton">
            <Popup x:Name="FilterBoxPopup"
                   PlacementTarget="{Binding ElementName=FilterButton}"
                   Placement="Bottom"
                   StaysOpen="False">
                <Border BorderBrush="Black"
                        Background="White"
                        Margin="2">
                    <ListView ItemsSource="{Binding ElementName=Self, Path=FilterList}"
                              x:Name="FilterListView"
                              Height="300"
                              Width="150">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <!--<CheckBox IsChecked="{Binding IsChecked}" />-->
                                    <!--<TextBlock Text="{Binding Name}" />-->
                                    <!--This is where I don't know how to properly bind eg. the above control, things I tried:-->
                                    <!--<TextBlock Text="{Binding ElementName=FilterListView, Path=FilterElementName}" />-->
                                    <!--<TextBlock Text="{Binding ElementName=Self, Path=DataContext.FilterElementName}" />-->
                                    <!--<TextBlock Text="{Binding ElementName=FilterListView, Path=DataContext.FilterElementName}" />-->
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Border>
            </Popup>
        </Button>
        <TextBlock Grid.Column="3"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Left"
                   Margin="10"
                   Text="{Binding ElementName=Self, Path=SelectedNames}" />
    </Grid>
</UserControl>

这是UserControl的使用方式,FilterElementName="Name"是我想设置的,取决于与FilterList列表绑定的列表:

<local:BaseFilterUserControl FilterList="{Binding Owners}"
                             FilterElementName="Name"
                             SelectedNames="{Binding SelectedNames}"/>

在这种情况下,所有者是所有者的简单 IReadOnlyList class。所有者 class 有一个字符串名称 属性。但是我将再次使用这个 UserControl 和不同的列表,例如。我想在其中使用版本列表的 Release 属性(对于 UserControl 中的 TextBlock):

<local:BaseFilterUserControl FilterList="{Binding Versions}"
                             FilterElementName="Release"
                             SelectedNames="{Binding SelectedReleases}"/>

ListView 已正确填充项目,因此 FilterList DependencyProperty 正在运行。但是嵌套控件仅在我对绑定进行硬编码时才起作用:

<TextBlock Text="{Binding Name}" />

为此,您需要将 TextBlocks 文本绑定的 Path-属性 绑定到 UserControl 的 FilterElementName 属性。不幸的是,Binding class 的 Path property 不是 DependencyProperty,因此不可绑定。

实现目标的一种方法是使用 ListView 的 DisplayMemberPath 属性,它是可绑定的:

   <ListView x:Name="FilterListView"
              Width="150"
              Height="300"
              ItemsSource="{Binding ElementName=Self, Path=FilterList}"
              DisplayMemberPath="{Binding ElementName=self, Path=FilterElementName}"/>

如果这种方法不起作用,因为您需要指定一个更复杂的 ItemTemplate,另一种方法是在您的 UserControl 中创建一个 属性 类型的 DataTemplate,将其用作 ListView 中的 ItemTemplate 并从外面像这样:

    <local:BaseFilterUserControl FilterList="{Binding Versions}"
                                 SelectedNames="{Binding SelectedReleases}">
        <local:BaseFilterUserControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Release}" />
            </DataTemplate>
        </local:BaseFilterUserControl.ItemTemplate>
    </local:BaseFilterUserControl>