2014-07-21 35 views
0

我有一个名为组的表,如下图所示:看着像如何绑定DataGrid中的ComboBox?

enter image description here

后上面我想你可能已经明白,主键和外键在同一个表中存在。我认为这就是开发者所称的循环引用。

在MainWindow.xaml我有一个DataGrid,其中包含三列,即组名,父母名称,说明。 XAML中的样子:

<Window .......> 

    <Window.DataContext> 
     <self:MainWindowViewModel /> 
    </Window.DataContext> 

    <DataGrid ItemsSource="{Binding Groups}" TabIndex="1"> 

     <DataGrid.Columns> 

      <DataGridTemplateColumn Header="Group Name" Width="2*"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding GroupName}"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
       <DataGridTemplateColumn.CellEditingTemplate> 
        <DataTemplate> 
         <TextBox Text="{Binding GroupName}" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellEditingTemplate> 
      </DataGridTemplateColumn> 

      <DataGridTemplateColumn Header="Parent" Width="2*"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding ParentID}" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
       <DataGridTemplateColumn.CellEditingTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding DataContext.GroupsCollection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" 
            SelectedValue="{Binding ParentID}" 
            DisplayMemberPath="GroupName"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellEditingTemplate> 
      </DataGridTemplateColumn> 

      <DataGridTemplateColumn Header="Description" Width="2*"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Description}"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
       <DataGridTemplateColumn.CellEditingTemplate> 
        <DataTemplate> 
         <TextBox Text="{Binding Description}"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellEditingTemplate> 
      </DataGridTemplateColumn> 

     </power:PowerDataGrid.Columns> 

    </power:PowerDataGrid> 

</Window> 

现在我有一个视图模型称为MainWindowViewModel

public class MainWindowViewModel : INotifyPropertyChanged 
{ 
    public MainWindowViewModel() 
    { 
     SampleDBContext sampleDBContext = new SampleDBContext(); 
     Groups = new ObservableCollection<Group>(); 
     GroupsCollection = new ObservableCollection<Group>(from g in sampleDBContext.Groups select g); 
    } 

    private ObservableCollection<Group> _groups; 
    public ObservableCollection<Group> Groups 
    { 
     get 
     { 
      return _groups; 
     } 
     set 
     { 
      _groups = value; 
      OnPropertyChanged("Groups"); 
     } 
    } 

    private ObservableCollection<Group> _groupsCollection; 
    public ObservableCollection<Group> GroupsCollection 
    { 
     get 
     { 
      return _groupsCollection; 
     } 
     set 
     { 
      _groupsCollection = value; 
      OnPropertyChanged("GroupsCollection"); 
     } 
    } 

    #region INotifyPropertyChanged 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(string propertryName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertryName)); 
     } 
    } 

    #endregion 
} 

问题:

Output : 

enter image description here

,你可以在上面的图片中看到当我按TAB或Enter后在父列中选择一个组,下一个单元格被聚焦,但单元格在父列下有一个红色的大纲表示出现了一些错误。单元格也不会离开编辑模式。我也在输出窗口中获得很多绑定错误。所以,我知道我的绑定是不正确的。有人可以帮我在datagrid里绑定Combobox吗?

更新:

如果我使用如下的SelectedValuePath:

<ComboBox ItemsSource="{Binding DataContext.GroupsCollection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" 
      SelectedValue="{Binding ParentID}" SelectedValuePath="{Binding GroupID}" 
      DisplayMemberPath="GroupName" /> 

然后错误消失和细胞也留下编辑模式。但是,然后TextBlock(这是celltemplate)总是保持空白。

+0

基本上,你的情况已经在[this](http://stackoverflow.com/a/5409984/4614937)答案中解决了。 – goncharenko

回答

0

解决了它。我不应该在SelectedValuePath中使用绑定。所以,现在我的代码应该是这个样子:

<ComboBox ItemsSource="{Binding DataContext.GroupsCollection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" 
      SelectedValue="{Binding ParentID}" SelectedValuePath="GroupID" 
      DisplayMemberPath="GroupName" /> 

现在的问题是:

我得到的ID(但我想有名称,而不是ID)组合框的的SelectedValue的编辑模式结束后,由于我的如下CellTemplate的结合:

<DataGridTemplateColumn Header="Parent" Width="2*"> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding ParentID}" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
    <DataGridTemplateColumn.CellEditingTemplate> 
     <DataTemplate> 
      <ComboBox ItemsSource="{Binding DataContext.GroupsCollection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" 
         SelectedValue="{Binding ParentID}" SelectedValuePath="GroupID" 
         DisplayMemberPath="GroupName" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellEditingTemplate> 
</DataGridTemplateColumn> 

所以我创建了一个转换器,如下:

public class GroupIDToGroupName : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value != null) 
     { 
      SampleDBContext sampleDBContext = new SampleDBContext(); 
      return (from g in sampleDBContext.Groups 
        where g.GroupID == (int)value 
        select g.GroupName).FirstOrDefault(); 
     } 
     else 
     { 
      return ""; 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     SampleDBContext sampleDBContext = new SampleDBContext(); 
     return (from g in sampleDBContext.Groups 
       where g.GroupName == (string)value 
       select g.GroupID).FirstOrDefault(); 
    } 
} 

我宣布,转换器的App.xaml为:

<self:GroupIDToGroupName x:Key="GroupIDToGroupNameConveerter" /> 

现在,我的手机模板的样子:

<TextBlock Text="{Binding ParentID, Converter={StaticResource GroupIDToGroupNameConveerter}}" /> 

我不知道,我在正确的方式解决我的问题或不不。但它的工作!

如果有人有一个好主意,那么请分享...........