我遇到了将ObservableCollection绑定到WPF GridView的问题。 ObservableCollection填充了自定义Objekt(用户)。 然后我得到ObservableCollection并尝试根据本教程将ist绑定到DataGrid: ObservableCollection in WPF
这是我的用户类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfVPNM.UserObjects
{
public class Users
{
/*Enthält die einzelnen Benutzer mit detailierteren Informationen*/
private String sam {get;set;}
private String fname {get;set;}
private String lname {get;set;}
private String active {get;set;}
#region constructor
public Users(String sam, String active) {
this.sam = sam;
this.active = active;
}
public Users(String sam, String active, String fname, String lname)
{
this.sam = sam;
this.active = active;
this.fname = fname;
this.lname = lname;
}
#endregion
#region Getter/Setter
public String getsam() { return this.sam; }
public String getfname() { return this.fname; }
public String getlname() { return this.lname; }
public String getactive() { return this.active; }
#endregion
}
}
这是我的班级ObservableCollection
:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfVPNM.UserObjects
{
public class UsersInGroup
{
/* Objekt welches die einzelnen Benutzer in den Gruppen enthält */
public ObservableCollection<Users> UserList = new ObservableCollection<Users>();
/*Fügt der Liste einen neuen Benutzer hinzu*/
public void Add(String sam, String active) {
this.UserList.Add(new Users(sam, active));
}
public ObservableCollection<Users> getList()
{
return this.UserList;
}
}
}
这里我们有XAML定义,基于上面命名的教程:
<ListView x:Name="lstNames" Margin="1,68,0,1" Grid.RowSpan="2">
<ListView.View>
<GridView x:Name="grdNames">
<GridViewColumn Header="sam" DisplayMemberBinding="{Binding sam}"/>
<GridViewColumn Header="active" DisplayMemberBinding="{Binding active}"/>
</GridView>
</ListView.View>
</ListView>
最后在后台我想将ObservableCollection绑定到GridView:
public void renewTableView() {
/*Beschaffe die Gruppenmitglieder der Gruppe grp_vpn*/
ADFunctions adf = new ADFunctions();
UserObjects.UsersInGroup groupMembers = adf.getGroupMember(DCEntry);
ObservableCollection<UserObjects.Users> list = groupMembers.getList();
foreach (UserObjects.Users us in list)
{
Debug.WriteLine("List - " + us.getsam() + ";" + us.getactive()) ;
}
lstNames.ItemsSource = list;
//dgaccounts.ItemsSource = groupMembers.getList();
Debug.WriteLine("Table AHOI");
}
使用foreach循环,我检查ObserableCollection
是否填充了数据,是的。然后我想设置网格视图的源,但它不显示任何数据。有人可以帮助我或给我一个好的工作指导吗?
那会很棒:)
答案 0 :(得分:1)
我建议您使用MVVM模式,而不是依赖于后面的代码。
因此,您需要创建一个ViewModel
来实现INotifyPropertyChanged
并将其设置为您的视图的DataContext
。
举一个简单的例子,您的ViewModel
看起来像:
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public ObservableCollection<User> Users { get; set; }
public ViewModel()
{
Users = new ObservableCollection<User>()
{
new User { Sam = "1", Active = "True", FName ="Test First Name 1", LName ="Test Last Name 1"},
new User { Sam = "2", Active = "True", FName ="Test First Name 2", LName ="Test Last Name 2"},
};
}
}
然后将其设置为View类构造函数中的DataContext
(这是执行此操作的最简单方法):
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
最后将DataGrid
绑定到ObservableCollection
:
<ListView ItemsSource="{Binding Users}">
<ListView.View>
<GridView AllowsColumnReorder="true">
<GridViewColumn DisplayMemberBinding="{Binding Path=Sam}" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Active}" Width="100" />
</GridView>
</ListView.View>
</ListView>
希望这有帮助!
答案 1 :(得分:0)
您的属性sam,active等是私有的。
如果您想要只读属性,通常会将它们声明为:
private string _sam;
public string Sam
{
get
{
return _sam;
}
}
我强烈建议您查看MVVM模式,其中您没有从代码
引用UI元素