Question : Silverlight Master Details

Hi

I created a master details scenario where a row selected from the datagrid(Master) is displayed in the detail section. The detail section has few combo boxes and textboxes. When I made changes in the details section, I could see the changes get reflected from textbox to data grid but not from combo box. I tried several way like datacontext,selecteditem and changed bindings unsuccessfully. Any solutions/approaches are highly appreciated.(Below is the sample code).

------------------------------------------------------------MainPage.xaml----------------------------------------------

<UserControl x:Class="ComboBoxPractice.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

    <StackPanel Width="600" Height="600">
        <data:DataGrid Name="dtgdMaster" SelectionChanged="DataGrid_SelectionChanged"  Width="500" Height="300">

        </data:DataGrid>

        <Grid Name="grdDetail">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150"></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Row="0" Grid.Column="0" Text=" Id:" Margin="10"></TextBlock>
            <TextBox Name="txtId" Text="{Binding Path=id,Mode=TwoWay}" Grid.Row="0" Grid.Column="1" Margin="10" HorizontalAlignment="Left" ></TextBox>

            <TextBlock  Grid.Row="1" Grid.Column="0" Text="Step:" Margin="10"></TextBlock>
            <ComboBox  Name="cboStep" Grid.Row="1" Grid.Column="1" Margin="10" HorizontalAlignment="Left" SelectedItem="{Binding Path=step,Mode=TwoWay}"></ComboBox>

            <TextBlock Grid.Row="2" Grid.Column="0" Text="Name:" Margin="10"></TextBlock>
            <ComboBox Name="cboName" Grid.Row="2" Grid.Column="1" Margin="10" HorizontalAlignment="Left" SelectedItem="{Binding Path=name,Mode=TwoWay}"></ComboBox>

        </Grid>

 
    </StackPanel>

</UserControl>

----------------------------------------------------------MainPage.xaml.cs-------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace ComboBoxPractice
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }
        ObservableCollection<Employee> Employees;
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            Employees = new ObservableCollection<Employee>{
            new Employee{id="1",name="n1",step="s1"},
            new Employee{id="2",name="n2",step="s2"},
            new Employee{id="3",name="n3",step="s3"},
            new Employee{id="4",name="n4",step="s4"}
            };
            dtgdMaster.ItemsSource = Employees;

            cboName.ItemsSource = Employees;
            cboName.DisplayMemberPath = "name";
            cboName.DataContext = selectedEmployee;

            cboStep.ItemsSource = Employees;
            cboStep.DisplayMemberPath = "step";
            cboName.DataContext = selectedEmployee;
        }

        Employee selectedEmployee;
        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            object selectedEmployeeObject = (sender as DataGrid).SelectedItem;
            selectedEmployee = (Employee)selectedEmployeeObject;

            txtId.DataContext = selectedEmployee;
            cboName.SelectedItem = selectedEmployee;          
            cboStep.SelectedItem = selectedEmployee;
        }

       
    }

    public class Employee:INotifyPropertyChanged
    {
        private string _id;
        private string _name;
        private string _step;
        public string id
        {
            set
            {
                _id = value;
                OnPropertyChanged("id");
            }
            get
            {
                return _id;
            }
        }
       
        public string name
        {
            set
            {
                _name = value;
                OnPropertyChanged("name");
 
            }
            get
            {
                return _name;
            }
        }
        public string step
        {
            set
            {
                _step = value;
                OnPropertyChanged("step");
            }
            get
            {
                return _step;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string parameterName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this,new PropertyChangedEventArgs(parameterName));
            }
        }

        #endregion
    }
}

 

------------------------------------------------------------------------

 

Thank You

Answer : Silverlight Master Details

Random Solutions  
 
programming4us programming4us