2012-05-21 118 views
0

這一定很簡單,但我似乎錯過了一些東西。我搜索了幾個小時,但沒有遇到任何可以解決我的問題的東西。問題是,雖然當我嘗試編輯DataGrid的價值觀之一,我可以將我的LINQ查詢到WPF DataGrid中,我得到以下錯誤:將WPF DataGrid綁定到LINQ查詢(實體框架)

System.InvalidOperationException was unhandled Message='EditItem' is not allowed for this view. Source=PresentationFramework StackTrace: at System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item) at System.Windows.Controls.DataGrid.EditRowItem(Object rowItem) at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e) at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(Object sender, ExecutedRoutedEventArgs e) at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)

我的DataGrid中的XAML看起來是這樣的:

<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="565" HorizontalAlignment="Left" Margin="6,92,0,0" Name="translatedStringsDataGrid1" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="602"> 
    <DataGrid.Columns> 
    <DataGridTextColumn x:Name="stringsIDColumn2" Binding="{Binding Path=StringsID}" Header="Strings Name" Width="SizeToHeader" /> 
    <DataGridTextColumn x:Name="translatedStringsValueColumn1" Binding="{Binding Path=TranslatedStringsValue}" Header="Translated Strings Value" Width="SizeToHeader" /> 
    </DataGrid.Columns> 
</DataGrid> 

我的組合框的這樣的SelectedChange事件做一個LINQ查詢:

private void cbSelectLang_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 

    var query = from o in _context.TranslatedStrings.Local 
       where o.LanguagesID == cbSelectLang.SelectedIndex + 1 
       join r in _context.Strings.Local on o.StringsID equals r.StringsID into SubSet2 

       from s in SubSet2.DefaultIfEmpty() 

       select new { StringsID = s.StringsName, TranslatedStringsValue = o.TranslatedStringsValue }; 

    this.translatedStringsDataGrid1.ItemsSource = query; 

} 

我使用「POCO實體」如果有人認爲有實現這個更簡單的方法。我真的覺得我錯過了一些非常基本和明顯的東西,如果有人願意指出我的意思! :-)

非常感謝。

回答

1

我還沒有測試過這個,但我相當確定你的問題是因爲你從你的查詢中返回一個匿名類型。嘗試將其更改爲

... 
from s in SubSet2.DefaultIfEmpty() 
select new MyRealType 
{ 
    StringsID = s.StringsName, 
    TranslatedStringsValue = o.TranslatedStringsValue 
}; 

您需要在其中定義MyRealType。

+0

是的,謝謝菲爾,這是我在 –

0

部分由於菲爾·我現在有一個可行的技術,它涉及一個ObservableCollection和新持有人類型:

private class JoinClass 
    { 
     public string StringsID { get; set; } 
     public string TranslatedStringsValue { get; set; } 
    } 

    private void cbSelectLang_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     ObservableCollection<JoinClass> collection = new ObservableCollection<JoinClass>(); 

     var query = from o in _context.TranslatedStrings.Local 
           where o.LanguagesID == cbSelectLang.SelectedIndex + 1 
           join r in _context.Strings.Local on o.StringsID equals r.StringsID into SubSet 
           from s in SubSet.DefaultIfEmpty() 
           select new JoinClass { StringsID = s.StringsName, TranslatedStringsValue = o.TranslatedStringsValue }; 

     foreach (var item in query) 
     { 
      collection.Add(item); 
     } 

     this.translatedStringsDataGrid1.ItemsSource = collection; 
    } 

謝謝!

+0

下面給出的答案的一部分,你可以更簡單地做「var collection = new ObservableCollection (query.ToList());」 – Phil