WPF-UI绑定如何实现?为什么这个示例代码不起作用
这是我的xmal主窗口:WPF-UI绑定如何实现?为什么这个示例代码不起作用,wpf,binding,Wpf,Binding,这是我的xmal主窗口: <Window x:Class="HR1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <
<Window x:Class="HR1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel HorizontalAlignment="Left" Width="162">
<ListView ItemsSource="{Binding EventsCollection}" Background="AliceBlue" Height="311" >
<ListView.View>
<GridView>
<GridViewColumn Header="event Date" Width="112"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
<ScrollViewer>
<StackPanel Margin="116,0,0,0" Width="284">
<ListView Name="employeeListView" ItemsSource="{Binding EmployeeCollection}">
<ListView.View>
<GridView>
<GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding Path=EmployeeID}" Width="80"/>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=FirstName}" Width="80"/>
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=LastName}" Width="80"/>
<!--<GridViewColumn Header="start" DisplayMemberBinding="{Binding Path=startHR}" Width="67" />
<GridViewColumn Header="finish" DisplayMemberBinding="{Binding Path=finishHR}" Width="67"/>-->
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</ScrollViewer>
</Grid>
}
这是第二个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace HR1.Model
{
class employeeData : INotifyPropertyChanged
{
public employeeData(long ID,string f,string l){
EmployeeID = ID;
LastName = l;
FirstName = f;
}
#region privete Members
private long employeeID;
private string firstName;
private string lastName;
#endregion
#region proprties
public long EmployeeID
{
get
{
return employeeID;
}
set
{
employeeID = value;
if (value != this.employeeID)
{
this.employeeID = value;
NotifyPropertyChanged("EmployeeID");
}
}
}
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
if (value != this.firstName)
{
this.firstName = value;
NotifyPropertyChanged("FirstName");
}
}
}
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
if (value != this.lastName)
{
this.lastName = value;
NotifyPropertyChanged("LastName");
}
}
}
#endregion
#region events
public event PropertyChangedEventHandler PropertyChanged;
#endregion
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
注意:在我的观点中,我声明
雇员收集
以及xmal系列:
<ListView Name="employeeListView" ItemsSource="{Binding EmployeeCollection}">
我做错了,因为如果您先将其更改为:
并将x:Name=window添加到根窗口元素,那么它是否工作?将主窗口构造函数更新为:
public MainWindow()
{
InitializeComponent();
initlalizeEmployeeList();
initlalizeEventDateList();
this.DataContext = this;
}
我发现了问题所在 我必须这样做 NotifyPropertyChangedsomelist 例如:
private void initlalizeEventDateList()
{
IList<employeeData> list = new List<employeeData>();
//query to database should be here
foreach (employeeData dataString in employeeDatesString)
{
list.Add(dataString );
}
employeeCollection = new CollectionView(list);
NotifyPropertyChanged("employeeCollection");
}
不,你也不知道:如果其他人觉得这篇文章有帮助,那么添加新的内容对他会有帮助……好的,我已经改变了我的帖子。我可以建议您尝试将代码缩减到最简单的情况来演示您的问题吗?我不知道问题是什么,我尝试使用:this绑定到我的代码变量:public CollectionView EmployeeCollection但它不起作用。。。我在Constructor中给EmployeeCollection实例值,但它仍然不起作用。。。。我没有切换到其他用户控件。。。它不起作用…你说它不起作用是什么意思?两个列表视图中是否没有显示任何项目?
<Grid DataContext="{Binding ElementName=window}">
public MainWindow()
{
InitializeComponent();
initlalizeEmployeeList();
initlalizeEventDateList();
this.DataContext = this;
}
private void initlalizeEventDateList()
{
IList<employeeData> list = new List<employeeData>();
//query to database should be here
foreach (employeeData dataString in employeeDatesString)
{
list.Add(dataString );
}
employeeCollection = new CollectionView(list);
NotifyPropertyChanged("employeeCollection");
}