1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to synchronize a comboxes in bound wpf datagrid

Discussion in 'C#' started by falcon eyes, Apr 9, 2013.

  1. falcon eyes

    falcon eyes New Member

    Joined:
    Apr 8, 2007
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Hi all
    I've created an application that consist of two objects,one of them its name Site and defined as below
    Code:
    [Serializable]   
        public class CSite:INotifyPropertyChanged,ISerializable
            {
               public CSite()
                            {  }
                 #region Fields Section
                    string  _siteName;
                    int     _siteID;
                    int     _siteZone;
                    string  _siteLocation;
                    string  _siteConfiguration;
                 #endregion
           [field:NonSerialized]
                public event PropertyChangedEventHandler PropertyChanged;
                void OnPropertyChanged(string probName)
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs(probName));
                    }
                }
    
     #region Properties Section
                public string SiteName
                {
                    get { return _siteName; }
                    set
                    {
                        _siteName = value;
                        OnPropertyChanged("SiteName");
                    }
                }
    
                public int SiteZone
                {
                    get { return _siteZone; }
                    set
                    {
                        _siteZone = value;
                        OnPropertyChanged("SiteZone");
                    }
                }
    
    
                public int SiteID
                {
                    get { return _siteID; }
                    set
                    {
                        _siteID = value;
                        OnPropertyChanged("SiteID");
                    }
                }
    
                public string SiteLocation
                {
                    get { return _siteLocation; }
                    set
                    {
                        _siteLocation = value;
                        OnPropertyChanged("SiteLocation");
                    }
                }
    
                public string SiteConfiguration
                {
                    get { return _siteConfiguration; }
                    set
                    {
                        _siteConfiguration = value;
                        OnPropertyChanged("SiteConfiguration");
                    }
                }
     #endregion
               
              #region Serialization Section
                //for reading stream
                  CSite(SerializationInfo info, StreamingContext context)
                {
                    SiteName = (string)info.GetString("siteName");
                    SiteZone = (int)info.GetInt32("siteZone");
                    SiteID = (int)info.GetInt32("siteID");
                    SiteLocation = (string)info.GetString("siteLocation");
                    SiteConfiguration = (string)info.GetString("siteConfiguration");
                }
                //for writing stream
                public void GetObjectData(SerializationInfo info, StreamingContext context)
                {
                    info.AddValue("siteName", SiteName);
                    info.AddValue("siteZone", SiteZone);
                    info.AddValue("siteID", SiteID);
                    info.AddValue("siteLocation", SiteLocation);
                    info.AddValue("siteConfiguration", SiteConfiguration);
                }
     #endregion
            }
        }
    
    and the other its name CSiteReport defined as below
    Code:
      [Serializable]
       public class CSiteReport:INotifyPropertyChanged,ISerializable
    
       {
     #region Fields Section
           CSite _issuedSite;       /// <summary> Site having a problem  </summary>  
           string _issue;           /// <summary> Describe the Problem of the Site</summary>
           DateTime _issueDate;     /// <summary> Date Of happend Problem</summary>
           DateTime _actionDate;    /// <summary> Date Of repair Day</summary>
           string _action;          /// <summary> The actions taked for repairing</summary>
           string _actionResult;    /// <summary> the result of repairing proccess</summary>
           ObservableCollection<CActionEng> _actionTeam;    /// <summary>The Repairing team</summary>
          
     #endregion
    
           //
           public CSiteReport()
           {
               _actionTeam = new ObservableCollection<CActionEng>();
            //    sitesIDS = new ConstantCollections.SitesIDs();
              _issuedSite=new CSite();       /// <summary> Site having a problem  </summary> 
                                             /// 
    
              _issueDate = DateTime.Now;
              _actionDate = DateTime.Now;
           }
    
            [field: NonSerialized]
            public event PropertyChangedEventHandler PropertyChanged;
    
         
            void onSRPropertyChanged(string probName)
           {
             if(PropertyChanged!=null)
             {
              PropertyChanged(this,new PropertyChangedEventArgs(probName));
             }
           }
    
          
     #region Proerties Section
          
            public CSite IssuedSite
            {
                get { return _issuedSite;}
                set
                {
                    _issuedSite = value;
                    onSRPropertyChanged("IssuedSite");
                }
            }
           
           public string Issue
           {
               get { return _issue; }
               set
               {
                   _issue = value;
                   onSRPropertyChanged("Issue");
               }
           }
    
           public DateTime IssueDate
           {
               get { return _issueDate; }
               set
               {
                   _issueDate = value;
                   onSRPropertyChanged("IssueDate");
               }
           }
    
           public DateTime ActionDate
           {
               get { return _actionDate; }
               set
               {
                   _actionDate = value;
                   onSRPropertyChanged("ActionDate");
               }
           }
    
           public string ActionTaken
           {
               get { return _action; }
               set
               {
                   _action = value;
                   onSRPropertyChanged("ActionTaken");
               }
           }
    
           public string ActionResults
           {
               get { return _actionResult; }
               set
               {
                   _actionResult=value;
                   onSRPropertyChanged("ActionResults");
               }
           }
    
           public ObservableCollection<CActionEng> ActionTeam
           {
               get { return _actionTeam; }
               set
               {
                   _actionTeam = value;
                   onSRPropertyChanged("ActionTeam");
                 
               }
           }
          
                     
        public CActionEng ActionEngineer
           {
             //  get { return _actionEng.EngName; }
               set { 
                 
                   ActionTeam.Add(value ); 
                   onSRPropertyChanged("ActionEngineer"); 
               }
           }
            
    
             public CActionEng RemoveActionEng
             {
                 set 
                 {
                     ActionTeam.Remove(value);
                     onSRPropertyChanged("RemoveActionEng"); 
                 }
             }
        
     #endregion
     
     #region Serialization Sction
           //for reading stream
             CSiteReport(SerializationInfo info, StreamingContext context)
           {
               IssuedSite = (CSite)info.GetValue("issuedSite", typeof(CSite));
               Issue = info.GetString("problem");
               IssueDate = info.GetDateTime("problemDate");
               ActionDate = info.GetDateTime("repairDate");
               ActionTaken = info.GetString("repairSteps");
               ActionResults = info.GetString("actionResults");
               ActionTeam = (ObservableCollection<CActionEng>)info.GetValue("workTeam", typeof(ObservableCollection<CActionEng>));
           }
           //for writing stream
           public void GetObjectData(SerializationInfo info, StreamingContext context)
           {
               info.AddValue("issuedSite",IssuedSite);
               info.AddValue("problem",Issue);
               info.AddValue("problemDate",IssueDate);
               info.AddValue("repairDate",ActionDate);
               info.AddValue("repairSteps",ActionTaken);
               info.AddValue("actionResults",ActionResults);
               info.AddValue("workTeam",ActionTeam,typeof(ObservableCollection<CActionEng>));
           }
    #endregion
        
       }
    
    and in the main window file i've added the line
    Code:
    this.DataContext=this 
    and defined a property
    Code:
      public CSitesReportsContainer SitesContainers
            {
                get { return _SitesManagers.SitesRepsColl; }
                set
                {
                    _SitesManagers.SitesRepsColl = value;
                    onPropertyChanged("SitesContainers");
                }
            }
    
    and i've created two observable collection,one of type Site its name Sites,and the other of type CSiteReport and its name CSitesReportCollection
    and bound both to DataGrid that its rows consist of comboboxes as below
    Code:
    xmlns:Collections="clr-namespace:SitesReporter.Proj_Classes.ConstantCollections" 
       <Window.Resources>
       		Collections:ActionsTeam x:Key="AT"></Collections:ActionsTeam>
          <Collections:Sites x:Key="tetsites"></Collections:Sites>
       </Window.Resources> 
       <DataGrid Grid.Row="2" Name="mainGrid" CellEditEnding="mainGrid_CellEditEnding_1"
                      ItemsSource="{Binding}" 
                      DataContext="{Binding  SitesContainers}"
                      AutoGenerateColumns="False" 
                      IsSynchronizedWithCurrentItem="True" 
                      CanUserAddRows="True" Grid.ColumnSpan="2" 
                      Margin="4,0,0,5" Grid.RowSpan="2" 
                      SelectionChanged="mainGrid_SelectionChanged" >
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="ID">
                        <DataGridTemplateColumn.CellTemplate >
                            <DataTemplate>
                                <TextBlock Text="{Binding IssuedSite.SiteID}"></TextBlock>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <ComboBox  Name="idcombx" 
                                           ItemsSource ="{Binding Source={StaticResource tetsites},Path=SitesColl}"
                                           DisplayMemberPath="SiteID"
                                           SelectedValuePath="SiteID"
                                           SelectedValue="{Binding IssuedSite.SiteID}" 
                                >
                                </ComboBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>
              <DataGrid.Columns>      
    
    i've included one column defination for simplcity but i have the follwing columns
    -Site Zone
    -Site Name
    -Site Configuratios
    -Site Location
    -Issue

    my question is
    How to make the comboboxes of one row(that belong to the Site object only) to be synchronized with each other?
    by mean
    when i select an item from comobox (that belong to the Site object only) it automaticly select the items in the others comboxes (that belong to the Site object only) with the same index in the same row.
    Regards
     

Share This Page