已经在主细节模式中有两个类,
大师班有一个细节大师的集合,并且需要(想要)内联执行所有粗略的选项。
课程:
public partial class Items
{
public Items()
{
this.Detail = new HashSet<Detail>();
}
public decimal NumIdConcepto { get; set; }
public string StrDescripcionConcepto { get; set; }
public virtual ICollection<Detail> Detail { get; set; }
}
public partial class Detail
{
public int IntTipoAsociado { get; set; }
public decimal NumIdConcepto { get; set; }
public virtual Items Items { get; set; }
public virtual Master Master { get; set; }
}
public partial class Master
{
public Master()
{
this.Detail = new HashSet<Detail>();
}
public int IntTipoAsociado { get; set; }
public string StrDescripcionTipoAsociado { get; set; }
public virtual ICollection<Detail> Detail { get; set; }
}
出于编辑目的和限制 EF 生成器必须执行从HashSet到ObservableCollection的转换以获取 Master 的 Detail 集合
public class listToObservableCollection : BaseConverter, IValueConverter
{
public HashSet<Detail> JustForPath { get; set; }
public ObservableCollection<Detail> JustForPathObservable { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
HashSet<Detail> observableList = (HashSet<Detail>)value;
JustForPathObservable = new ObservableCollection<Detail>(observableList);
return JustForPathObservable;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
JustForPath = (HashSet<Detail>)value;
return JustForPath;
}
}
public abstract class BaseConverter : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
使用这种结构,我想生成一个 DataGrid,它允许我添加、删除和编辑 Detail In line,不想调用其他表单
像这样:
我的 XAML 代码如下所示:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModels="clr-namespace:Contratos.ViewModels" x:Class="Contratos.Views.TipoAsociado"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
xmlns:l="clr-namespace:Recursos;assembly=Recursos"
Title="{Binding Title}" Height="Auto" Width="Auto">
<Window.DataContext>
<ViewModels:TipoAsociadoVM/>
</Window.DataContext>
<Window.Resources>
<l:listToObservableCollection x:Key="listoToObservable"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<DataGrid Grid.Row="1"
ItemsSource="{Binding Master}"
AutoGenerateColumns="False"
SelectionMode="Single"
GridLinesVisibility="Vertical"
CanUserAddRows="True"
CanUserDeleteRows="True"
x:Name="GridTipoAsociado"
Margin="5" SelectionChanged="GridTipoAsociado_SelectionChanged">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command="{Binding _ICommandSelectionChange}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Description" x:Name="Description">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding StrDescripcionTipoAsociado}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding StrDescripcionTipoAsociado }"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid Grid.Row="1"
ItemsSource="{Binding Path=Detail,Converter={StaticResource listoToObservable},Mode=TwoWay}"
DisplayMemberPath="StrDescripcionConcepto"
AutoGenerateColumns="False"
IsSynchronizedWithCurrentItem="True"
SelectionMode="Single"
SelectionUnit="Cell"
GridLinesVisibility="Vertical"
CanUserAddRows="True"
CanUserDeleteRows="True"
x:Name="GridItems"
Margin="20,5,5,5">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Items">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Items.StrDescripcionConcepto}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<!--
Here is my problem How can i to bind to the propper values to perform Editing
Without having this exception "TWO WAY BINDING REQUIRES PATH OR XPATH" when try to update DETAIL items:
-->
<TextBox Text="{Binding Items.StrDescripcionConcepto}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
</Window>
现在,我需要有关如何构建适当的 CellEditingTemplate 的帮助,以允许我编辑我需要的所有更改?
这是我的问题如何在尝试更新 DETAIL 项目时绑定到属性值以执行编辑而没有此异常“TWO WAY BINDING REQUIRES PATH OR XPATH”?
注意:我不想在这里创建新项目,只需将其附加到Detail