我無法將DataContext從一個userControl後面的代碼設置爲另一個userControl。我無法將DataContext從一個userControl的代碼設置爲另一個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);
}
我有兩個用戶控件DashboardSynopsisView
和AuditInfoView
具有視圖模型DashboardSynopsisViewModel
和AuditInfoViewModel
。所以在後面的代碼中,我有一個事件grdWorkingList_MouseDoubleClick
它是在鼠標雙擊它將內容控件dashboardSynopsisView
設置爲AuditInfoView
以及它的DataContext
到AuditInfoViewModel
。 AuditInfoViewModel
有一個屬性AuditDTO
我正在使用它來顯示所選項目的信息。
這裏是我的.xaml文件
<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>
的一部分,這裏是我已經解決我的問題的變化。
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);
}