从ItemsControl DataTemplate绑定到父UserControls DependencyPropert



我想创建一个用户控件,其中包含带按钮的ItemsControl,我想将它们的内容绑定到用户控件依赖属性(类似DisplayMemberPath属性(。

我的xaml.cs代码:

public partial class ButtonsItemsSourceControl : UserControl
{
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(object), typeof(ButtonsItemsSourceControl), null);
    public static readonly DependencyProperty DisplayMemberProperty = DependencyProperty.Register("DisplayMember", typeof(string), typeof(ButtonsItemsSourceControl), null);
    public object ItemsSource
    {
        get { return (ICollection<object>)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }
    public string DisplayMember
    {
        get { return (string)GetValue(DisplayMemberProperty); }
        set { SetValue(DisplayMemberProperty, value); }
    }
    public ButtonsItemsSourceControl()
    {
        InitializeComponent();
    }
}

我的xaml代码:

<UserControl x:Class="Controls.ButtonsItemsSourceControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480" x:Name="root">
<ItemsControl x:Name="ctrl" ItemsSource="{Binding ItemsSource, ElementName=root}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content= ?????/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
</UserControl>

我应该在Content属性中编写什么绑定表达式才能执行此操作?

为了清楚起见,您是否在询问如何将子属性绑定到父属性?

RelativeSource

最新更新