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?
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>
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 whatGetEmployee
DataContext
inDataGrid
and if it has theGetEmployee
property. You wrote<DataGrid ItemsSource=...
, okay, theItemsSource
was found, and whatDataContext
it has, you just have to guess. - VladD September