In the comments I provided a link where they answer my question, but I don’t understand how he ties the selected entity in the combobox to the desired entity (Stelug to plavka), but I can’t ask - there are not enough turnips.

Program on C # WPF MVVM pattern. There is an entity detail and employee (LinqTOSQL) and they are related. I want to output detail in the datagrid in a separate column of workers in the form of a combobox (DataGridComboBoxColumn) so that they can be changed for this entity. Found it like, made like http://www.sql.ru/forum/1169706/ne-bindyatsya-dannye-k-combobox-v-wpf-datagrid

ComboBox appeared and the data, too, but when you change an element, it becomes red (the exception seems to be) and is not attached to the entity (because I did not attach, but I do not understand). Surely you need to register it in one of the properties. But which one? enter image description here

I give the code of the column itself

<DataGridComboBoxColumn DisplayMemberPath="FullName" SelectedValuePath="idemployee" SelectedValueBinding="{Binding employee}" Header="ФИО отв.лица"> <DataGridComboBoxColumn.ElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.GetEmployee}"/> <Setter Property="IsReadOnly" Value="True"/> </Style> </DataGridComboBoxColumn.ElementStyle> <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.GetEmployee}"/> </Style> </DataGridComboBoxColumn.EditingElementStyle> </DataGridComboBoxColumn> 

I give VM code

 public class DetailVM:ViewModelBase { private TechDepDBDataContext _techDepDB; public DetailVM(TechDepDBDataContext db) { this._techDepDB = db; _details = new ObservableCollection<detail>() { new detail { NameDetail = "ara", CountDetail = 2 } }; } public string EssenceName { get { return "Деталь"; } } //public DTG_Detail UEdtg //{ //get {return _uedtg;} // set { _uedtg = value; } //} #region Read/Update private List<officememo> _officememo; public List<officememo> Select { get { _officememo = (from c in _techDepDB.officememo select c).ToList(); return _officememo; } } #endregion public List<employee> GetEmployee { get { var emp= (from c in _techDepDB.employee select c).ToList(); return emp; } } #region Create private ObservableCollection<detail> _details; public ObservableCollection<detail> InsertDetailCol { get { OnPropertyChanged("_details"); return _details; } } public void Insert() { _techDepDB.detail.InsertAllOnSubmit(_details); _details.Clear(); _techDepDB.SubmitChanges(); } #endregion } 

VM primary class code public class MainVM: ViewModelBase {private TechDepDBDataContext _db; ObservableCollection _pages;

  public ObservableCollection<ViewModelBase> Pages { get { return _pages; } set { _pages = value; } } public MainVM() {_db=new TechDepDBDataContext(); _db.LoadOptions = GetLoadOption(); _pages = new ObservableCollection<ViewModelBase>(); _pages.Add(new DetailVM(_db)); _pages.Add(new OperationVM(_db)); _pages.Add(new OfficeMemoVM(_db)); _pages.Add(new DeviceVM(_db)); _pages.Add(new ProjectVM(_db)); } 

XAML MainView Markup

 <Window x:Class="TechDepCRM.MainView" x:Name="MainV" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainView" Height="535" Width="778" xmlns:viewModel="clr-namespace:TechDepCRM.ViewModels" xmlns:view="clr-namespace:TechDepCRM.Views" DataContext="{Binding MainVM,ElementName=MainV}"> <Window.Resources> <DataTemplate DataType="{x:Type viewModel:DetailVM}"> <view:DetailView /> </DataTemplate> <DataTemplate DataType="{x:Type viewModel:OperationVM}"> <view:OperationView /> </DataTemplate> <DataTemplate DataType="{x:Type viewModel:OfficeMemoVM}"> <view:OfficeMemoView /> </DataTemplate> <DataTemplate DataType="{x:Type viewModel:DeviceVM}"> <view:DeviceView /> </DataTemplate> <DataTemplate DataType="{x:Type viewModel:ProjectVM}"> <view:ProjectView /> </DataTemplate> </Window.Resources> <TabControl Name="Tabs" ItemsSource="{Binding Pages}" Margin="10,36,0,0"> <TabControl.ItemContainerStyle> <Style TargetType="TabItem"> <Setter Property="Header" Value="{Binding EssenceName}" /> </Style> </TabControl.ItemContainerStyle> </TabControl> 

UPDATE 9/16/16

Datagrid tag

 <DataGrid ItemsSource="{Binding ElementName=CBOfficeMemo, Path=SelectedItem.detail}" 

ComboBox to which DataGrid binds (under "Filter" on the form)

 <ComboBox Name="CBOfficeMemo" ItemsSource="{Binding Select}" HorizontalAlignment="Left" Margin="58,119,0,0" VerticalAlignment="Top" Width="120" Height="19"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding NumOfficeMemo}"></TextBlock> <TextBlock Text="\"></TextBlock> <TextBlock Text="{Binding YearOfficeMemo}"></TextBlock> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> 
  • What is the minus? If it was often discussed, please drop me a link. - Arantler
  • I correctly understand that in ItemsSourse we thrust our employees, and in the SelectedValuePath and SelectedValueBinding we bind just by the connection to the detail detail? (Detail.employee) - Arantler
  • Found a similar question [ ru.stackoverflow.com/questions/298651/…. This is my case, but I don’t understand how it binds to an entity and through what. - Arantler
  • You do not need to be attached to entities at all. Entities are a model, and you need to bind to a VM. - VladD
  • 2
    Well, look. You have written in DataGridComboBoxColumn : <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.GetEmployee}"/> (by the way, what about the style, and not directly? ItemsSource is also in DataGridComboBoxColumn). And now I need to dig out your code, which is what GetEmployee DataContext in DataGrid and if it has the GetEmployee property. You wrote <DataGrid ItemsSource=... , okay, the ItemsSource was found, and what DataContext it has, you just have to guess. - VladD September

1 answer 1

Found a solution to the problem (background in chatika). Thanks VladD.

After checking the interface, I carefully studied Output and came across a strange line:

 "System.Windows.Data Error: 23 : Cannot convert '10' from type 'Int32' to type 'TechDepCRM.employee' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: TypeConverter не может выполнить преобразование из System.Int32." 

This appeared when choosing employee and leaving a line / cell.

There was a question "Where is the conversion?".

I learned in figure 10 my employee ID.

"Where do I even work with an identifier?"

Here in this line, SelectedValuePath = "idemployee" From what I read earlier (pancake), we can say that through this property we see an appeal to the ComboBox element (did not quite understand, but advised to use it to work with elements via id) In the end, first trying to change value on Entity, employee. Completely removed it and it all worked. In bd everything is recorded.

Answer

Remove the SelectedValuePath line or use it correctly