我正在使用mvvm在silverlight 5中创建一个项目。
我在一个页面中有一个组合框和一个数据网格。这里问题意味着,"如果我在组合框中选择任何值,它不会将所选行更新到数据网格。
我在datagrid中绑定三个字段Fname,Sname,Dept。并且我将所有dept值绑定到
组合框。
Datagrid结构就是这个,
____________________________
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;
}
}
这里wat是问题的意思, 如果我选择ComboBox中的任何项目,选择的记录将不会绑定到DataGrid ..! 请帮我绑定一下。提前感谢你..
答案 0 :(得分:0)
您好我刚才找到了解决方案。
我们需要告诉值从modelview更改为视图。要做到这一点,只需给出
<强> RaisePropertyChanged( “雇员”); 强>
**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);
}
RaisePropertyChanged("Employees");
}
问题解决了......:)