C# WPF控件组列表数据绑定到自定义类列表?
我对WPF还是有点陌生(只做了一些小项目)。我试图创建一个重复的控件组(用户可以添加/删除这些组),并将其数据绑定到自定义类。示例用户界面:C# WPF控件组列表数据绑定到自定义类列表?,c#,wpf,user-interface,data-binding,controls,C#,Wpf,User Interface,Data Binding,Controls,我对WPF还是有点陌生(只做了一些小项目)。我试图创建一个重复的控件组(用户可以添加/删除这些组),并将其数据绑定到自定义类。示例用户界面: ([UserButton1] [UserButton2]) <--each of these () is a separate group of buttons ([Cheese] [Wine] ) ([Wallace] [Gromit] ) [Add] <-
([UserButton1] [UserButton2]) <--each of these () is a separate group of buttons
([Cheese] [Wine] )
([Wallace] [Gromit] )
[Add] <--this button can add more groups
比如
List<UserButtons> = {
[0]: UserButton1, UserButton2
[1]: Cheese, Wine
[2]: Wallace, Gromit
}
列表={
[0]:用户按钮1,用户按钮2
[1] :奶酪、葡萄酒
[2] :华莱士,格罗米特
}
我知道创建WPF就是为了做这类事情,但我不太清楚该怎么做
我应该使用某种列表视图吗?数据模板会有帮助吗?StackPanel听起来不错,但它没有用于列表的数据绑定…或者是吗?我甚至不知道如何使数据绑定为上面所示的按钮组工作(如果这对您来说是有意义的话…很抱歉这个坏例子)。有人对这个问题有什么见解吗
我搜索试图找到一个与此相关的问题,但没有看到,可能是因为我不确定要搜索什么。因此,如果这是一次意外的欺骗,我很抱歉。我不完全确定您在寻找什么,但我希望下面的示例能有所帮助。我使用了一个ItemsControl,其ItemsSource设置为UserButtons的集合。其ItemTemplate属性设置为显示两个按钮的StackPanel,每个按钮的内容属性都绑定到UserButtons中的属性 XAML:
添加
代码隐藏:
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
}
公共部分类主窗口:窗口
{
可观察收集oc;
公共主窗口()
{
初始化组件();
oc=新的ObservableCollection()
{
新建UserButtons(){Button1=“UserButton1”,Button2=“UserButton2”},
新用户按钮(){Button1=“Cheese”,Button2=“Wine”},
新用户按钮(){Button1=“Wallace”,Button2=“Gromit”},
};
this.itemsControl.ItemsSource=oc;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
添加(新用户按钮(){Button1=“NewButton1”,Button2=“NewButton2”});
}
}
公共类UserButtons:INotifyPropertyChanged
{
私有字符串按钮1;
公共字符串按钮1
{
获取{返回this.button1;}
设置
{
此.button1=值;
本条关于财产变更(“按钮1”);
}
}
私有字符串按钮2;
公共字符串按钮2
{
获取{返回this.button2;}
设置
{
此.button2=值;
本条关于财产变更(“按钮2”);
}
}
#区域INotifyProperty更改成员
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propName)
{
if(this.PropertyChanged!=null)
{
this.PropertyChanged(this,newpropertyChangedEventArgs(propName));
}
}
#端区
}
我不完全确定您在寻找什么,但我希望下面的示例能有所帮助。我使用了一个ItemsControl,其ItemsSource设置为UserButtons的集合。其ItemTemplate属性设置为显示两个按钮的StackPanel,每个按钮的内容属性都绑定到UserButtons中的属性
XAML:
添加
代码隐藏:
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
}
公共部分类主窗口:窗口
{
可观察收集oc;
公共主窗口()
{
初始化组件();
oc=新的ObservableCollection()
{
新建UserButtons(){Button1=“UserButton1”,Button2=“UserButton2”},
新用户按钮(){Button1=“Cheese”,Button2=“Wine”},
新用户按钮(){Button1=“Wallace”,Button2=“Gromit”},
};
this.itemsControl.ItemsSource=oc;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
添加(新用户按钮(){Button1=“NewButton1”,Button2=“NewButton2”});
}
}
公共类UserButtons:INotifyPropertyChanged
{
私有字符串按钮1;
公共字符串按钮1
{
获取{返回this.button1;}
设置
{
此.button1=值;
本条关于财产变更(“按钮1”);
}
}
私有字符串按钮2;
公共字符串按钮2
{
获取{返回this.button2;}
设置
{
此.button2=值;
本条关于财产变更(“按钮2”);
}
}
#区域INotifyProperty更改成员
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propName)
{
if(this.PropertyChanged!=null)
{
this.PropertyChanged(this,newpropertyChangedEventArgs(propName));
}
}
#端区
}
非常感谢您!你完全理解我解释得不好的问题,并为我指明了正确的方向。谢谢没问题。很乐意帮忙非常感谢你!你完全理解我解释得不好的问题,并为我指明了正确的方向。谢谢没问题。很乐意帮忙
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
}