0
我想用組合框列創建雙向可編輯數據網格。 此數據網格連接到.NET實體框架。我怎麼能這樣做?你能延長我的代碼嗎?組合框綁定到實體數據庫的DataGrid字段
SQL:
CREATE TABLE Languages(
L_Id INTEGER PRIMARY KEY IDENTITY(1,1),
L_Name VARCHAR(16) NOT NULL
);
CREATE TABLE Movies (
M_Id INTEGER PRIMARY KEY IDENTITY(1,1),
M_Title VARCHAR(128) NOT NULL,
M_Language_Id INTEGER NOT NULL,
CONSTRAINT MoviesToLanguages
FOREIGN KEY (M_Language_Id)
REFERENCES Languages (L_Id)
);
INSERT INTO Languages VALUES('subtitled');
INSERT INTO Languages VALUES('synchronized');
MainWindow.xaml
...
<DataGrid Name="MoviesDataGrid" Grid.Row="1" AutoGenerateColumns="False" ItemsSource="{Binding Movies}">
<DataGrid.Columns>
<DataGridTextColumn Header="Title" Binding="{Binding M_Title}" />
<DataGridTemplateColumn Header="Language" CanUserReorder="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Center" Text="{Binding Path=Languages.L_Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
???
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
...
CinemaViewModel.cs
private CinemaEntities _Entities;
public ObservableCollection<Movies> Movies{ get; private set; }
public CinemaViewModel()
{
_Entities = new CinemaEntities();
Movies = new ObservableCollection<Movies>(_Entities.Movies.Include("Languages"));
OnPropertyChanged("Movies");
}
private void Save()
{
_Entities.SaveChanges();
}
我在此: <組合框 \t的ItemsSource = 「{結合DataContext.Movies,的RelativeSource = {的RelativeSource FindAncestor,AncestorType =數據網格}}」 \t的SelectedItem = 「{結合M_Language_Id}」 \t的DisplayMemberPath =「{結合Languages.L_Name}」 /> 但我想改變數值後錯誤: * deferrefresh沒有一個AddNew或edititem交易*過程中允許 什麼問題? – BlackCat
我不認爲這與組合框相關。另外,如果要在組合框中顯示語言,請將其「ItemSource」設置爲語言列表,而不是電影 –
另外,displaymemberpath應該是屬性名稱,而不是綁定。 –