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