【问题标题】:1:n bind to DataGridComboBoxColumn1:n 绑定到 DataGridComboBoxColumn
【发布时间】:2014-07-03 10:34:36
【问题描述】:

我想在 DataGrid (CollectioViewSource) 中显示 1:n 关系。基于EntityFramework。

到目前为止的作品: 显示基本数据,显示DataGridComboBoxColumn,可以选择DataGridComboBoxColumn中的其他条目。

不起作用: 使用 _context.SaveChanges() 保存更改; (_context 是 DatabaseContext 的一个实例)。 因此,如果我更改 DataGridComboBoxColumn 中的其他类型并尝试确保安全,我得到: "System.InvalidOperationException: 属性 'TypId' 是对象的关键信息的一部分,不能更改'。(从德语到英语的自己的翻译......)

信息:我可以直接在数据库中更改 Operation.TypId 属性而不会出错。

XAML:

    <Page x:Class="ProdPlanNET.Views.OperationPage"
   <Page.Resources>

    <CollectionViewSource x:Key="operationViewSource"/>
</Page.Resources>
<Grid >
    <Grid.Resources>
        <CollectionViewSource x:Key="source" Source="{Binding Types, Mode=OneTime}"/>
    </Grid.Resources>

    <DataGrid DataContext="{StaticResource operationViewSource}" AutoGenerateColumns="False" Margin="8,112,8,8" IsReadOnly="False" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn IsReadOnly="True" Binding="{Binding OperationId}"  
                            Header="Operation Id" Width="SizeToHeader"/>
            <DataGridTextColumn Binding="{Binding Name}"  
                            Header="Operation Name" Width="SizeToHeader"/>

            <DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource source}}"
                                    SelectedValuePath="TypId" DisplayMemberPath="Name" SelectedValueBinding="{Binding Path=Typ.TypId}" />
     </DataGrid.Columns>

我的视图模型:

   namespace ProdPlanNET.ViewModels
   {
      class OperationPageViewModel : BaseViewModel
{

    public CollectionViewSource cvs = new CollectionViewSource();
    private DatabaseContext _context = new DatabaseContext();

    public ObservableCollection<Typ> Types
    {
        get
        {
            return _context.Types.Local;
        }
        set { }
    }

    System.Windows.Data.CollectionViewSource operationViewSource;

    public OperationPageViewModel(object viewSource)
    {
        this.operationViewSource = ((System.Windows.Data.CollectionViewSource)viewSource);

        _context.Types.Load();
        _context.Workplaces.Load();
        _context.Operations.Load();

        operationViewSource.Source = _context.Operations.Local;

    }
    public void SaveCommand()
    {    

            _context.SaveChanges();
   }}}

【问题讨论】:

    标签: entity-framework data-binding datagridcomboboxcolumn


    【解决方案1】:

    我自己弄的。

      <DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource source}}" 
                      SelectedValueBinding="{Binding Typ, Mode=TwoWay}" DisplayMemberPath="Name" />
    

    希望它对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-04-24
      • 2015-01-21
      • 2013-09-05
      • 2011-07-10
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 2011-12-21
      • 2014-01-04
      相关资源
      最近更新 更多