【发布时间】:2009-05-25 07:09:00
【问题描述】:
我想在 WPF 中创建一个UserControl,我想通过它公开一个集合属性。我想根据集合的变化来改变UserControl的UI。
例如,假设我有一个绑定到我的UserControl 的字符串集合。基于该集合,我想在 UserControl 上创建按钮,其中包含这些文本作为按钮文本。有什么办法可以实现吗?
【问题讨论】:
标签: .net wpf user-controls user-interface
我想在 WPF 中创建一个UserControl,我想通过它公开一个集合属性。我想根据集合的变化来改变UserControl的UI。
例如,假设我有一个绑定到我的UserControl 的字符串集合。基于该集合,我想在 UserControl 上创建按钮,其中包含这些文本作为按钮文本。有什么办法可以实现吗?
【问题讨论】:
标签: .net wpf user-controls user-interface
是的,您可以设置一个DataTemplate,其中包含一个用于绑定到该集合的ItemsControl 控件的按钮。例如:
//For code:
items.DataContext = new List<string>
{
"Item 1",
"Item 2",
"Item 3"
};
//For XAML
<ItemsControl x:Name="items" ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
【讨论】: