2011-01-05 90 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