首页 > 解决方案 > 绑定 Datagrid ComboBox MVVM 模型

问题描述

我有一个 WPF 数据组合框绑定问题。
我已经阅读了很多关于这个的页面......值得几天,所以是时候发布一个问题了。

我在 Visual Studio 2019 中工作。我正在使用由实体框架生成的 MVVM 模型。我有两个 SQL 表,它们看起来像这样...... SQL 表

我创建了一个模型(名称:EmployeeModel)来管理后端数据。我还为主窗口(检查表)创建了一个模型(名称:SourceInspectionView)。

EmployeeModel一个状态类,如下所示:

namespace Sample3
{
    using System;
    using System.Collections.ObjectModel;

    public partial class Status
    {
   [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Status()
        {
            this.SourceInspections = new ObservableCollection<SourceInspection>();
        }

        public int StatusID { get; set; }
        public string Status1 { get; set; }
        public Nullable<bool> Active { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ObservableCollection<SourceInspection> SourceInspections { get; set; }
    }
}

SourceInspectionView一个检查类,如下所示:

namespace Sample3
{
    using System;
    using System.Collections.ObjectModel;

    public partial class SourceInspectionView
    {
        public Nullable<int> StatusID { get; set; }
        public string CustomerName { get; set; }

    }
}

我的 WPF 表单称为Mainwindow2. 后面的代码如下所示:

 namespace Sample3
{
    /// <summary>
    /// Interaction logic for MainWindow2.xaml
    /// </summary>
    public partial class MainWindow2 : Window
    {
        private SourceInspectionEntities _context = new SourceInspectionEntities();
        private BackEnddata _context2 = new BackEnddata();



        public MainWindow2()
        {
            InitializeComponent();
        }


        private void ButtonSave(object sender, RoutedEventArgs e)
        {
            _context.SaveChanges();
            this.SourceManagerGrid.Items.Refresh();

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            System.Windows.Data.CollectionViewSource StatusviewSource =
          ((System.Windows.Data.CollectionViewSource)(this.FindResource("StatusViewSource")));
            _context2.Status.Load();
            StatusviewSource.Source = _context2.Status.Local;



            System.Windows.Data.CollectionViewSource MySourceInspectionsViewSource =
            ((System.Windows.Data.CollectionViewSource)(this.FindResource("SourceInspectionViewSource")));
            _context.SourceInspectionViews.Load();
            MySourceInspectionsViewSource.Source = _context.SourceInspectionViews.Local;




            this.SourceManagerGrid.Items.Refresh();


        }

        private void ButtonSettings(object sender, RoutedEventArgs e)
        {
            MainWindow MySettings = new MainWindow();
            MySettings.Show();
        }
    }
}

我的 Xaml 看起来像这样:

<DataGrid x:Name="SourceManagerGrid" DataContext="{StaticResource SourceInspectionViewSource}"  ItemsSource="{Binding}" AutoGenerateColumns="False" EnableRowVirtualization="True" RowDetailsVisibilityMode="VisibleWhenSelected"  HorizontalAlignment="Stretch" DockPanel.Dock="top" CanUserAddRows="False" CanUserDeleteRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding StatusID}"></DataGridTextColumn>
            <DataGridComboBoxColumn x:Name="Status"  Header="Status" Width="Auto" SelectedValueBinding="{Binding StatusID}" SelectedValuePath="" >
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox" >
                        <Setter Property="ItemsSource" Value="{Binding Source= {StaticResource StatusViewSource}}"/>
                        <Setter Property= "DisplayMemberPath" Value="Status1" />
                        <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Source= {StaticResource StatusViewSource}}"/>
                        <Setter Property="SelectedValuePath" Value="StatusID" />
                        <Setter Property= "DisplayMemberPath" Value="Status1" />
                        <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
                    </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
            <DataGridTextColumn x:Name="CustomerName" Binding="{Binding CustomerName}" Header="CustomerName" Width="auto" IsReadOnly="True" />
        </DataGrid.Columns>
    </DataGrid>

所以这里是问题:

  1. 主窗口表单中的数据应填充并显示为单词(开始,完成)的当前状态。
  2. 当用户单击下拉框时,他们应该会看到状态表中的选项列表为单词(已开始、已完成)。但是,我需要将 StatusID 存储在 SQl 表中。不是话。

我当前的代码将填充下拉列表并显示单词。但是,当组合框失去焦点时,它会再次变为空白。

我见过各种各样的解决方案,其中一些带有代码、模板列和直接的 XAML。

你能看看代码并告诉我我错过了什么吗?我不确定我是否有正确的绑定,我猜这是问题所在。

提前致谢。克里

标签: c#wpfxamldata-binding

解决方案


经过大量查找和对数据库中的数据进行一些测试后,我意识到存在一些数据库问题。

我纠正了这些问题,删除了额外的模型,将两个模型合二为一,然后清理了所有内容,这段代码现在可以工作了。我感谢任何看过它的人。

克里


推荐阅读