2013-02-26 78 views
1

我想創建一個包含ItemsControl和按鈕的用戶控件,並且我想將它們的內容綁定到用戶控件依賴項屬性(類似於DisplayMemberPath屬性)。從ItemsControl DataTemplate綁定到父UserControls DependencyProperty

我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> 

我應該在內容屬性寫什麼綁定表達式辦呢?

回答

0

只是爲了清楚起見,你問的是如何將一個子屬性綁定到父屬性?

RelativeSource

+0

謝謝你的回覆@justin。我忘了提及我在Windows Phone應用程序中執行此操作,並且在RelativeSource中沒有FindAncestor模式。只有TemplatedParent和Self模式... – Waldek 2013-02-26 23:37:07