我正在使用 mvvm 在 silverlight 5 中创建一个项目。我在一页中有一个组合框和一个数据网格。这是问题的意思,“如果我在组合框中选择任何值,它不会将所选行更新到数据网格。我在数据网格中绑定三个字段 Fname、Sname、Dept。我将所有的 Dept 值绑定到
组合框。数据网格结构是这样的,
____________________________
Fname | Sname | Dept |
---------------------------
jeba | Raj |Tester |
---------------------------
Raj | Kumar |Engineer|
____________________________
ComboBox 结构是这样的,
Tester
Engineer
如果我选择组合框 SelectionChanged 项是 Tester,则表示该记录将绑定到 Datagrid。
我写的代码如下,
XAML 代码是:
<my:DataGrid x:Name="McDataGrid" ItemsSource="{Binding Employees,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Margin="130,151,0,0" Height="99" VerticalAlignment="Top" RowBackground="#AA5D9324" AutoGenerateColumns="True" HorizontalAlignment="Left" Width="194" CanUserResizeColumns="True" HorizontalScrollBarVisibility="Disabled">
</my:DataGrid>
<ComboBox ItemsSource="{Binding Employees}" DisplayMemberPath="Dept" SelectedItem="{Binding Names, Mode=TwoWay}" Margin="130,117,0,0" Height="26" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<si:CallDataMethod Method="BindNew"/>
<si:SetProperty TargetName="LayoutRoot" PropertyName="Background" Value="PaleGoldenrod"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
ViewModel 类文件代码:
EmployeeListViewModel.cs
private Employee _Names;
public Employee Names
{
get
{
return _Names;
}
set
{
_Names = value;
RaisePropertyChanged("Names");
}
}
public void BindNew()
{
Employee emplo = new Employee();
Employees = Silverlight_MVVM.DataHelper.EmployeeDataHelper.EmployeeData();
var semp = from emp in Employees where emp.Dept.Equals(Names.Dept) select emp;
List<Employee> lt = new List<Employee>();
lt = semp.ToList();
lt.Count();
int i;
Employees.Clear();
for (i = 0; i < lt.Count(); i++)
{
emplo.Fname = lt[0].Fname.ToString();
emplo.Sname = lt[0].Sname.ToString();
emplo.Dept = lt[0].Dept.ToString();
Employees.Add(emplo);
}
}
这里 Employee.cs 是 Model: Employee.cs 的类文件:
public class Employee: INotifyPropertyChanged
{
//Constructor Creation Begins
#region Constructor
public Employee(string Fname = "", string Sname = "", string Dept = "")
{
_Fname = Fname;
_Sname = Sname;
_Dept = Dept;
}
#endregion
#region Properties
private string _Fname = string.Empty;
public string Fname
{
get { return _Fname; }
set
{
_Fname = value;
}
}
private string _Sname = string.Empty;
public string Sname
{
get { return _Sname; }
set
{
_Sname = value;
}
}
private string _Dept = string.Empty;
public string Dept
{
get { return _Dept; }
set
{
_Dept = value;
}
}
#endregion
#region INotifyPropertyChanged
// [field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
在 DataHelper 文件夹中,我有以下内容,
public static class EmployeeDataHelper
{
public static ObservableCollection<Employee> EmployeeData()
{
ObservableCollection<Employee> sampleEmployee = new ObservableCollection<Employee>();
sampleEmployee.Add(new Employee("jas", "manickaraj", "Engineer"));
sampleEmployee.Add(new Employee("raj", "Kumar", "Tester"));
sampleEmployee.Add(new Employee("Jeba", "raj", "Developer"));
return sampleEmployee;
}
}
这是问题的意思,如果我在 ComboBox 中选择任何项目,则所选记录将不会绑定到 DataGrid ..!请帮我绑定这个。提前谢谢你..