這一定很簡單,但我似乎錯過了一些東西。我搜索了幾個小時,但沒有遇到任何可以解決我的問題的東西。問題是,雖然當我嘗試編輯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實體」如果有人認爲有實現這個更簡單的方法。我真的覺得我錯過了一些非常基本和明顯的東西,如果有人願意指出我的意思! :-)
非常感謝。
是的,謝謝菲爾,這是我在 –