2011-01-05 135 views
1

我有一個嵌入在DataGrid中的ComboBox的WPF 4應用程序。 ComboBox位於模板列中,該列在編輯模式下顯示組合框,但僅在TextBlock中顯示。如果編輯單元格並從組合框中選取新值,則在離開單元格時,處於查看模式的TextBlock不會反映新值。最終,新值將被保存並在刷新窗口時顯示,但在網格中仍在編輯時不會發生。WPF 4 Datagrid與組合框

數據庫表結構如下:Employee(int employeeID,string last,string first,string phone); Project(int projectID,string projectName); ProjectMember(int projMemID,int projectID,int employeeID,字符串角色)。一個只讀屬性FullName被添加到Employee實體連接first + last。所以視圖TextBlock顯示ProjectMember.Employee.FullName。組合框項目來自Employee.FullName。

以下是使這更復雜的部分。網格和組合框綁定到與我的數據庫綁定的EnityFramework中的不同ItemsSource。對於這個問題,網格顯示項目成員。項目成員名稱可以從組合框中挑選出來,列出所有公司員工。

有關如何在指向不同DataSources時將DataGridColumnTemplate的視圖模式綁定到編輯值的任何想法?

有關XAML

<Window.Resources> 
    <ObjectDataProvider x:Key="EmployeeODP" /> 
</Window.Resources> 
<StackPanel> 
<DataGrid Name="teamProjectGrid" AutoGenerateColumns="false" ItemsSource="{Binding Path=ProjectMembers}" 
     <DataGrid.Columns> 
      <DataGridTemplateColumn Header="Name" x:Name="colProjectMember"> 
      <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Path=ProjectMemberFullName}" /> 
      </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
      <DataGridTemplateColumn.CellEditingTemplate> 
      <DataTemplate> 
       <ComboBox x:Name="ProjectMemberCombo" IsReadOnly="True" 
        DisplayMemberPath="FullName" 
        SelectedValue="{Binding Path=Employee}" 
        ItemsSource="{Binding Source={StaticResource EmployeeODP}}" 
       /> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellEditingTemplate> 
     </DataGridTemplateColumn> 
     <DataGridTextColumn x:Name="colProjectRole" Binding="{Binding Path=ProjectRole}" Header="Role" /> 
    </DataGrid.Columns> 
</DataGrid> 
</StackPanel> 

相關的代碼背後

​​

回答

0

只需更換一個組合框:

<ComboBox x:Name="ProjectMemberCombo" IsReadOnly="True" 
       DisplayMemberPath="FullName" SelectedValuePath="FullName" 
       SelectedItem="{Binding Path=Employee}" 
       SelectedValue="{Binding Path=ProjectMemberFullName}" 
       ItemsSource="{Binding Source={StaticResource EmployeeODP}}" 
      /> 

也是該酒店ProjectMemberFullName必須提高前夜nt PropertyChanged

另一種方式 - 替代的TextBlock:

<TextBlock Text="{Binding Path=Employee.FullName}" /> 

可以擺脫屬性的全名:

<TextBlock DataContext="{Binding Employee}"> 
    <Run Text="{Binding First}"/> <Run Text="{Binding Last}"/> 
</TextBlock> 

但我不知道,如果實體實現INotifyPropertyChanged接口。 在第二個變體中,員工必須致電OnNotifyPropertyChanged("Employee")

+0

感謝您的快速響應。我編輯了我的原始問題以提供數據庫表。儘管我仍在嘗試一些變化,但這兩種方法似乎都不起作用。首先,問題出現在SelectedValue周圍,因爲ProjectMemberFullName只是一個只讀屬性,它不支持雙向綁定。對於第二種情況,該實體支持INotifyPropertyChanged,但其行爲方式仍然相同。不知道爲什麼。 – Doug 2011-01-06 14:27:15

+0

ProjectMemberFullName是您寫的屬性。爲什麼你不能將它改爲讀寫屬性?同時檢查屬性的setter中的Employee屬性和pur斷點。 – vorrtex 2011-01-06 21:09:38

+0

使ProjectMemberFullName可讀寫。只要存在,二傳手甚至可以是「無所事事」。我不確定我瞭解所發生的事情的細節和時間,但會發生。我覺得我花更多時間去嘗試解決實體框架問題,而不是節省時間。再次感謝您的幫助。 – Doug 2011-01-07 14:22:20