C# 带有项目/数据模板的 WPF 自定义控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/278835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:18:15  来源:igfitidea点击:

WPF Custom Control with Item/Data Templates

c#wpfwpf-controlsuser-controls

提问by Justin Bozonier

I know how to create a custom user control in WPF but how can I make it so that someone can provide an ItemTemplate?

我知道如何在 WPF 中创建自定义用户控件,但如何制作它以便有人可以提供 ItemTemplate?

I have a user control that is a mixture of several other WPF controls, one of them being a ListBox. I'd like to let the user of the control specify the content of the list box but I'm not sure how to pass that information through.

我有一个用户控件,它混合了其他几个 WPF 控件,其中一个是 ListBox。我想让控件的用户指定列表框的内容,但我不确定如何传递该信息。

EDIT: The accepted answer works with the following correction:

编辑:接受的答案适用于以下更正:

<UserControl x:Class="WpfApplication6.MyControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:WpfApplication6">
    <ListBox ItemTemplate="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:MyControl}}, Path=ItemsSource}" />
</UserControl>

采纳答案by Todd White

You will want to add a DependencyProperty to your control. The xaml will look slightly different if you are deriving from UserControl or Control.

您需要将 DependencyProperty 添加到您的控件中。如果您从 UserControl 或 Control 派生,则 xaml 看起来会略有不同。

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

    public static readonly DependencyProperty ItemTemplateProperty =
        DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(MyControl), new UIPropertyMetadata(null));
    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate) GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty, value); }
    }
}

Here is xaml for a UserControl.

这是 UserControl 的 xaml。

<UserControl x:Class="WpfApplication6.MyControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:WpfApplication6">
    <ListBox ItemTemplate="{Binding ItemTemplate, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:MyControl}}}" />
</UserControl>

Here is xaml for a Control:

这是控件的 xaml:

<Style TargetType="{x:Type src:MyControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type src:MyControl}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">

                    <ListBox ItemTemplate="{TemplateBinding ItemTemplate}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>