2010-08-19 97 views
0

我对WPF还是有点新鲜感(只做过几个小项目)。我试图做一个重复的控件组(用户可以添加/删除这些组),将数据绑定到一个自定义类。例如UI: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吗? DataTemplate会有帮助吗?一个StackPanel听起来不错,但它没有数据绑定列表...或者它呢?而且我甚至不知道如何为上面所述的按钮组进行数据绑定工作(如果这对于您来说甚至是有意义的......对于这个不好的例子抱歉)。有没有人有任何关于这个问题的见解?

我搜索试图找到一个有关这个问题,并没有看到一个,也许是因为我不知道要搜索什么。所以,如果这是一个意外的欺骗,那么很抱歉。

回答

4

我不完全确定你在找什么,但我希望下面的例子有所帮助。我使用了ItemsControl的ItemsSource被设置为UserButtons集合的ItemsControl。它的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 
} 
+0

太谢谢你了!你完全理解我的解释不清的问题,并指出我正确的方向。谢谢! – NickAldwin 2010-08-19 20:54:03

+0

没问题。乐意效劳。 =) – ASanch 2010-08-19 20:56:44