0
votes

I am creating one project in silverlight 5 with mvvm. I have one combobox and one datagrid in one page. here wat's the problem means,"If i select any value in combobox it doesn't update the selected row to the datagrid. I bind three fields Fname, Sname, Dept in datagrid. and i bind all the dept value to
combobox. The Datagrid Structure is this,

____________________________
Fname   | Sname   | Dept   |
---------------------------
jeba    | Raj     |Tester  |
---------------------------
Raj     | Kumar   |Engineer|
____________________________

The ComboBox Structure is this,

Tester
Engineer

If i select the combobox SelectionChanged item is Tester means that record will be bind to Datagrid.

I wrote the code below is,

XAML Code is:

<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 Class File Code:

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);
            }
          }

Here Employee.cs is a class file for 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

In DataHelper Folder i have the following,

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;
        }
}

Here wat is the problem means, If i select any item in the ComboBox that selected record will not bind to DataGrid..! Please help me to bind this. Thank u in Advance..

1

1 Answers

0
votes

Hi i found the solution just now.

We need to tell the value is changed to the view from modelview. To do that, just give

RaisePropertyChanged("Employees");

**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");
          }

Problem Solved.. :)