2010-08-19 96 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