I am not able to set DataContext from code behind of one userControl to another userControl

StackOverflow https://stackoverflow.com/questions/22157512

  •  19-10-2022
  •  | 
  •  

Question

I am not able to set DataContext from code behind of one userControl to another userControl .

private void grdWorkingList_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e) {
    DashboardSynopsisViewModel dsViewModel = new DashboardSynopsisViewModel();
    AuditInfoViewModel auditInfoViewModel = new AuditInfoViewModel();

    AuditInfoView auditInfoView = new AuditInfoView();
    var selectedItem = (grdWorkingList.SelectedItem as AutoMgmtSoln.AuditWinPro.ClientData.Model.AuditDTO); 

    // MainWindow mainWindow = new MainWindow();
    DSViewContentControl.Content = new AuditInfoView();

    auditInfoView.DataContext = auditInfoViewModel;
    auditInfoViewModel.AuditDTO = auditInfoViewModel.getAuditById(selectedItem.AuditId); 
}

I have two user controls DashboardSynopsisView and AuditInfoView having view models DashboardSynopsisViewModel and AuditInfoViewModel. So in the code behind I have a event grdWorkingList_MouseDoubleClick which is fired on mouseDouble click which sets the content control of the dashboardSynopsisView to the AuditInfoView along with it's DataContext to AuditInfoViewModel. AuditInfoViewModel has a property AuditDTO which I am using to display information of the selected item.

Here is part of my .xaml file

<TextBlock Grid.Column="0" Grid.Row="0"  Text="Company Code :"></TextBlock>
<TextBlock Grid.Column="1" Grid.Row="0" Width="auto" Text="{Binding  AuditDTO.CompanyCode}" ></TextBlock>

<TextBlock Grid.Column="0" Grid.Row="1" >Company Name :</TextBlock>
<TextBlock Grid.Column="1" Grid.Row="1" Width="auto" Text="{Binding  AuditDTO.CompanyName}" ></TextBlock>

Here is the change I have made to resolve my the problem .

 private void grdWorkingList_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {

        DashboardSynopsisViewModel dsViewModel = new DashboardSynopsisViewModel();
        AuditInfoViewModel auditInfoViewModel = new AuditInfoViewModel();

        AuditInfoView auditInfoView = new AuditInfoView();
        var selectedItem = (grdWorkingList.SelectedItem as AutoMgmtSoln.AuditWinPro.ClientData.Model.AuditDTO); 


       // MainWindow mainWindow = new MainWindow();
        DSViewContentControl.Content = ***auditInfoView***;

        auditInfoView.DataContext = auditInfoViewModel;
        auditInfoViewModel.AuditDTO = auditInfoViewModel.getAuditById(selectedItem.AuditId); 

    }
Was it helpful?

Solution

You are setting the "auditInfoView" variable's data context, but setting the content control to a new AuditInfo view.

Make these match (probably by changing the content set to the auditInfoView variable) and your code should work.

To provide a little background, in case you are new to C#, using the new operator causes the class's constructor to be invoked, which creates a new object with default values (plus any sets or operations done by the constructor itself). Setting another instance's DataContext property will have no effect on the newly constructed instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top