如何在自定义用户控件中为 ListBox ItemTemplate 属性 设置适当的上下文

How to set a proper context for a ListBox ItemTemplate property in a custom user control

这是我的 ListBox 自定义控件:UCListBoxMainLabel 是 属性 我有问题。它用作 ListBox 项目的“标签”:

<ListBox ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource AncestorType=local:UCListBox}}"
         SelectedItem="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=local:UCListBox}}"
         >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding MainLabel, RelativeSource={RelativeSource AncestorType=local:UCListBox}}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

代码隐藏:

public partial class UCListBox : UserControl
{
    public UCListBox()
    {
        InitializeComponent();
    }

    public object ItemsSource
    {
        get { return (object)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(object), typeof(UCListBox), new PropertyMetadata(null));

    public object SelectedItem
    {
        get { return (object)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

    // Using a DependencyProperty as the backing store for SelectedItem.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedItem", typeof(object), typeof(UCListBox), new PropertyMetadata(null));


    public string MainLabel
    {
        get { return (string)GetValue(MainLabelProperty); }
        set { SetValue(MainLabelProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MainLabel.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MainLabelProperty =
        DependencyProperty.Register("MainLabel", typeof(string), typeof(UCListBox), new PropertyMetadata(string.Empty));
    }

这里我尝试在window中使用我的自定义控件:

<local:UCListBox
    ItemsSource="{Binding Participants}"
    SelectedItem="{Binding SelectedParticipant}"
    MainLabel ="{Binding NameShort1}"
/>

我收到绑定错误:

Property "NameShort1 is not found for WindowEditCaseVM.

MainLabel 属性 的上下文与其他属性一样是我的视图模型,而不是 ListBox 项的上下文。如何修复自定义控件 属性 的上下文以正确显示列表框项目?

在@Clemens 的帮助下帮助我解决问题的是将 DataTemplate 变成资源并为其定义 DataType 属性。

首先,定义一个命名空间,包含我的 Participant class 在我的 UserControl 的头部:

         xmlns:model="clr-namespace:DocConstructor.BusinessModel"

然后定义资源

<UserControl.Resources>
    <DataTemplate DataType="{x:Type model:Participant}">
        <StackPanel>
            <TextBlock Text="{Binding NameShort1}"/>
            <TextBlock Text="{Binding CaseStatusType.Description}"/>
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>

完整代码:

<UserControl x:Class="DocConstructor.UserControls.UCListBox"
             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" 
             xmlns:local="clr-namespace:DocConstructor.UserControls"
             mc:Ignorable="d" 
             xmlns:model="clr-namespace:DocConstructor.BusinessModel"
>
    <UserControl.Resources>
        <DataTemplate DataType="{x:Type model:Participant}">
            <StackPanel>
                <TextBlock Text="{Binding NameShort1}"/>
                <TextBlock Text="{Binding CaseStatusType.Description}"/>
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>
    <ListBox ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource AncestorType=local:UCListBox}}"
             SelectedItem="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=local:UCListBox}}"            
             >
    </ListBox>
</UserControl>