【问题标题】:Collection property for WPF UserControlWPF UserControl 的集合属性
【发布时间】:2009-05-25 07:09:00
【问题描述】:

我想在 WPF 中创建一个UserControl,我想通过它公开一个集合属性。我想根据集合的变化来改变UserControl的UI。

例如,假设我有一个绑定到我的UserControl 的字符串集合。基于该集合,我想在 UserControl 上创建按钮,其中包含这些文本作为按钮文本。有什么办法可以实现吗?

【问题讨论】:

    标签: .net wpf user-controls user-interface


    【解决方案1】:

    是的,您可以设置一个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>
    

    【讨论】:

      猜你喜欢
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多