【问题标题】:Binding from ItemsControl DataTemplate to parent UserControls DependencyProperty从 ItemsControl DataTemplate 绑定到父 UserControls DependencyProperty
【发布时间】:2013-02-26 22:50:53
【问题描述】:

我想创建一个包含带有按钮的 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 属性中写什么绑定表达式来做到这一点?

【问题讨论】:

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


    【解决方案1】:

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

    RelativeSource

    【讨论】:

    • 感谢您的回复@justin。我忘了说我是在 Windows Phone 应用程序中做的,RelativeSource 中没有 FindAncestor 模式。只有 TemplatedParent 和 Self 模式...
    猜你喜欢
    • 1970-01-01
    • 2020-12-29
    • 2014-07-30
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 2013-09-20
    相关资源
    最近更新 更多