为用户控件内的列表框设置项目模板

Set itemtemplate for a listbox inside usercontrol

我在 UserControl 中有一个列表框来管理具有不同属性的不同实体。 UserControl 对于所有实体都是相同的。 我使用 MVVM 并将通用 ViewModel 绑定为 UserControl 的 DataContext。 我希望为 UserControl 的容器 xaml 中的列表框设置 ItemTemplate 以显示实体属性。 对于实体 "Emlployee" 我需要显示 FullName,对于实体 Certifying 我需要显示 CertifyingAuthor 和 CertifyingDate 等等。

我需要的是类似的东西

    <StackPanel Grid.Row="0" Orientation="Vertical">
        <uc:SearchableFromListTextBox ItemTemplateForTheInsideListBox="{StaticResource Something}" ></uc:SearchableFromListTextBox>

我应该向 UserControl 添加一个 dependencyProperty ItemTemplateForTheInsideListBoxProperty 吗?我如何将它作为列表框的项目模板传递?

考虑到我的意大利母语,希望这个问题得到很好的解释。 谢谢

编辑:我放弃了。这是一个用于键盘数据输入的控件,类似于自动完成功能。 因为我被迫同意与 MVVM 妥协 :( 我会选择一些肮脏的方式来解决。 感谢大家

一个DataTemplateSelector可以做你想做的事。您可以从用户控件 XAML 中定义不同的模板,然后让选择器在其中进行选择。

假设这些模型 类:

public class Employee
{
    public Employee(string fullName)
    {
        FullName = fullName;
    }

    public string FullName { get; }
}

public class Certifying
{
    public Certifying(string certifyingAuthor, DateTime certifyingDate)
    {
        CertifyingAuthor = certifyingAuthor;
        CertifyingDate = certifyingDate;
    }

    public string CertifyingAuthor { get; }

    public DateTime CertifyingDate { get; }
}

您的用户控件 XAML 有一个 ListBox,它又使用模板选择器(我们稍后会讲到)——并且还为两个不同的对象定义了不同的模板模型类型:

<UserControl
    x:Class="WPF.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WPF"
    >
    <Grid>
        <ListBox x:Name="listBox">
            <ListBox.ItemTemplateSelector>
                <local:MyTemplateSelector>
                    <local:MyTemplateSelector.EmployeeTemplate>
                        <DataTemplate DataType="local:Employee">
                            <TextBlock
                                Foreground="Red"
                                Text="{Binding FullName}"
                            />
                        </DataTemplate>
                    </local:MyTemplateSelector.EmployeeTemplate>
                    <local:MyTemplateSelector.CertifyingTemplate>
                        <DataTemplate DataType="local:Certifying">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="120" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBlock
                                    Foreground="Blue"
                                    Text="{Binding CertifyingAuthor}"
                                />
                                <TextBlock
                                    Grid.Column="1"
                                    Foreground="Green"
                                    Text="{Binding CertifyingDate}"
                                />
                            </Grid>
                        </DataTemplate>
                    </local:MyTemplateSelector.CertifyingTemplate>
                </local:MyTemplateSelector>
            </ListBox.ItemTemplateSelector>
        </ListBox>
    </Grid>
</UserControl>

用户控件的代码隐藏,我只是将模型对象列表分配给列表框(为简单起见):

public partial class UserControl1
{
    public UserControl1()
    {
        InitializeComponent();
        listBox.ItemsSource = new List<object>
            {
                new Employee("Donald Duck"),
                new Certifying("Mickey Mouse", DateTime.Now),
                new Employee("Napoleon Bonaparte"),
                new Certifying("Homer Simpson", DateTime.Now - TimeSpan.FromDays(2)),
            };
    }
}

最后,模板选择器。它的两个属性是从用户控件的 XAML 设置的; SelectTemplate 的逻辑决定应用哪一个:

public class MyTemplateSelector : DataTemplateSelector
{
    /// <summary>
    /// This property is set from XAML.
    /// </summary>
    public DataTemplate EmployeeTemplate { get; set; }

    /// <summary>
    /// This property is set from XAML.
    /// </summary>
    public DataTemplate CertifyingTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item is Employee)
        {
            return EmployeeTemplate;
        }

        if (item is Certifying)
        {
            return CertifyingTemplate;
        }

        return base.SelectTemplate(item, container);
    }
}

并且,为了完整起见,用户控件的用法:

<Grid>
    <wpf:UserControl1 />
</Grid>

最终结果,当前选中拿破仑,鼠标悬停在荷马身上: