从ItemsControl DataTemplate绑定到父UserControls DependencyProperty

时间:2013-02-26 22:50:54

标签: wpf data-binding windows-phone-8

我想创建一个包含带有按钮的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属性中写什么绑定表达式来执行此操作?

1 个答案:

答案 0 :(得分:0)

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

RelativeSource