2014-07-21 62 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}}" /> 

我不知道,我在正確的方式解決我的問題或不不。但它的工作!

如果有人有一個好主意,那麼請分享...........