代码之家  ›  专栏  ›  技术社区  ›  NickAldwin

WPF控件组列表数据绑定到自定义类列表?

  •  0
  • NickAldwin  · 技术社区  · 16 年前

    我对WPF还是有点陌生(只做了几个小项目)。我正在尝试创建一个重复的控件组(用户可以添加/删除这些组),数据绑定到自定义类。实例用户界面:

    ([UserButton1] [UserButton2]) <--each of these () is a separate group of buttons
    ([Cheese]      [Wine]       )
    ([Wallace]     [Gromit]     )
                            [Add] <--this button can add more groups
    

    数据绑定到这样的类列表(伪代码):

    class UserButtons {
        string UserButton1 = "UserButton1"
        string UserButton2 = "UserButton2"
    }
    

    如

    List<UserButtons> = {
        [0]: UserButton1, UserButton2
        [1]: Cheese, Wine
        [2]: Wallace, Gromit
    }
    

    我知道这是WPF要做的事情,但我不太清楚该怎么做。

    我应该使用某种ListView吗?数据模板有帮助吗?StackPanel听起来不错,但它没有列表的数据绑定……还是?我甚至不知道如何使数据绑定对如上所示的按钮组起作用(如果这对您来说是有意义的话……对不起这个坏例子)。有人知道这个问题吗?

    我找了一个与此相关的问题,但没有看到,也许是因为我不确定要搜索什么。所以,如果这是一个意外的欺骗,我很抱歉。

    1 回复  |  直到 16 年前
        1
  •  4
  •   ASanch    16 年前

    我不完全确定你在找什么,但我希望下面的例子有帮助。我使用了一个itemsControl,它的itemsSource设置为用户按钮集合。它的itemTemplate属性设置为显示两个按钮的stackpanel,每个按钮的content属性都绑定到userbuttons中的属性。

    XAML:

    <Window x:Class="WpfApplication3.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication3"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
    
        </Window.Resources>
    
        <StackPanel Orientation="Vertical">
            <ItemsControl x:Name="itemsControl" Background="LightBlue">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Button Content="{Binding Button1}" Width="100"/>
                            <Button Content="{Binding Button2}" Width="100"/>
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
    
            <Button Width="50" Click="Button_Click">Add</Button>
    
        </StackPanel>
    
    </Window>
    

    代码落后:

    public partial class MainWindow : Window
    {
        ObservableCollection<UserButtons> oc;
    
        public MainWindow()
        {
            InitializeComponent();
    
            oc = new ObservableCollection<UserButtons>()
            {
                new UserButtons() { Button1="UserButton1", Button2 = "UserButton2"},
                new UserButtons() { Button1="Cheese", Button2 = "Wine"},
                new UserButtons() { Button1="Wallace", Button2 = "Gromit"},
            };
    
            this.itemsControl.ItemsSource = oc;
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            oc.Add(new UserButtons() { Button1 = "NewButton1", Button2 = "NewButton2" });
        }
    }
    
    public class UserButtons : INotifyPropertyChanged
    {
        private string button1;
        public string Button1
        {
            get { return this.button1; }
            set
            {
                this.button1 = value;
                this.OnPropertyChanged("Button1");
            }
        }
    
        private string button2;
        public string Button2
        {
            get { return this.button2; }
            set
            {
                this.button2 = value;
                this.OnPropertyChanged("Button2");
            }
        }
    
        #region INotifyPropertyChanged Members
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
    
        #endregion
    }