通过数据绑定WPF/MVVM从数据库填充的组合框中获取选定的值,以进行比较

我是 C# 初学者;该项目是在 Visual Studio (wpf) 中使用实体框架和视图模型制作的。我仅使用数据库中的“员工”姓名填充了组合框。我想要做的是获取变量中选定的项目值(或直接将其与数据库中存储的名称进行比较,以获取与该名称关联的所有其他表列;在我的情况下,我有年龄、属性等)


如果我直接进入MainWindow.xaml.cs,我可以轻松使用


Object selectedItem = ComboBoxName.SelectedItem; 它完成了工作。但是,我需要在我的MainWindowsViewModel.cs. 我发现SelectedItem="{Binding ..., Mode=...}"并尝试了几种解决方案,但找不到任何可行的解决方案。


我的 XAML 中的组合框:


 <ComboBox x:Name="comboPersonal" Grid.Column="6" HorizontalAlignment="Left" Margin="13,60,-62,0" Grid.Row="1" VerticalAlignment="Top" Width="183" ItemsSource="{Binding ang1}" SelectedItem="{Binding Path=Employers, Mode=TwoWay}"  />

组合框通过以下方式填充:


 public IList<string> ang1

        {

            get

            {

                using (ProbaHotel_1Entities db = new ProbaHotel_1Entities())

                {

                    var employee = db.Personal.Select(ang => ang.Nume).ToList();




                    return employee.ToList();

                }

            }

        }

在我的 MainWindowViewModel 中(这大部分是错误的):


 public IList<string> Employers

        {

            get

            {

                using (ProbaHotel_1Entities db = new ProbaHotel_1Entities())

                {

                    var vNume = db.Personal.Select(ang => ang.Nume);

                    this.NumeText = vNume.ToString();


                }

                return this.Employers;

            }


            set { }

        }

NumeText是文本框绑定到的变量。当我从组合框中选择一个项目时,我希望将该值传输到文本框。Select 语句错过了WHERE与selected valuenames in the database


文本框XAML:


<TextBox x:Name="text_nume" HorizontalAlignment="Left" Height="23"  TextWrapping="Wrap" Text="{Binding NumeText}" VerticalAlignment="Top" Width="89" Grid.Column="1" Grid.Row="0" />

在我的 MainWindowViewModel 中:


private string numeText;

        public string NumeText

        {

            get

            {

                return this.numeText;

            }

            set

            {


                this.numeText = value;

            }

        }


蛊毒传说
浏览 132回答 1
1回答

慕妹3242003

您可以使用包含模型的 List 并绑定到组合框项目源,并且可以绑定 DisplayMemberPath 来表示“Name”(模型的属性)并将 SelectedItem 绑定到该模型。请看代码。我使用了虚拟字符串值。在您的情况下,这应该是从数据库填充的,正如您提到的。请注意,在 SelectedEmployee 的设置器中,我根据您的要求为 NumText 分配值。一旦选定的项目发生更改,它将被分配并显示在 XAML 文本框中在您的情况下,您错误地将 SelectedItem 分配给 list 。它应该与您绑定的itemsource相关。您创建了单独的列表,一个用于项目源,另一个用于所选项目。而且我在代码中看不到与 INotifyPropertyChanged 相关的属性设置器的任何更改。XAML:<Window x:Class="WpfApplication13.MainWindow"&nbsp; &nbsp; &nbsp; &nbsp; xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&nbsp; &nbsp; &nbsp; &nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&nbsp; &nbsp; &nbsp; &nbsp; xmlns:d="http://schemas.microsoft.com/expression/blend/2008"&nbsp; &nbsp; &nbsp; &nbsp; xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"&nbsp; &nbsp; &nbsp; &nbsp; xmlns:local="clr-namespace:WpfApplication13"&nbsp; &nbsp; &nbsp; &nbsp; mc:Ignorable="d"&nbsp; &nbsp; &nbsp; &nbsp; Title="MainWindow" Height="350" Width="525">&nbsp; &nbsp; <Grid>&nbsp; &nbsp; &nbsp; &nbsp; <Grid.RowDefinitions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <RowDefinition Height="50*"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <RowDefinition Height="50*"/>&nbsp; &nbsp; &nbsp; &nbsp; </Grid.RowDefinitions>&nbsp; &nbsp; &nbsp; &nbsp; <TextBox x:Name="text_nume" HorizontalAlignment="Center" Height="23"&nbsp; TextWrapping="Wrap" Text="{Binding NumeText}" VerticalAlignment="Center" Width="89" Grid.Row="0" />&nbsp; &nbsp; &nbsp; &nbsp; <ComboBox x:Name="comboPersonal" Grid.Row="1" Height="30" Width="100" HorizontalAlignment="Center" DisplayMemberPath="Name" VerticalAlignment="Center" Margin="13,60,-62,0"&nbsp; ItemsSource="{Binding Ang1}" SelectedItem="{Binding SelectedEmployee}"&nbsp; />&nbsp; &nbsp; </Grid></Window>视图模型:&nbsp;public class ViewModel : INotifyPropertyChanged&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; #region Constants and Enums&nbsp; &nbsp; &nbsp; &nbsp; #endregion&nbsp; &nbsp; &nbsp; &nbsp; #region Private and Protected Member Variables&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; private IList<EmployeeModel> ang1;&nbsp; &nbsp; &nbsp; &nbsp; EmployeeModel _selectedEmployee;&nbsp; &nbsp; &nbsp; &nbsp; private string numeText;&nbsp; &nbsp; &nbsp; &nbsp; #endregion&nbsp; &nbsp; &nbsp; &nbsp; #region Private and Protected Methods&nbsp; &nbsp; &nbsp; &nbsp; private void OnPropertyChanged(string propName)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; #endregion&nbsp; &nbsp; &nbsp; &nbsp; #region Constructors&nbsp; &nbsp; &nbsp; &nbsp; public ViewModel()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ang1 = new List<EmployeeModel>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ang1.Add(new EmployeeModel() {&nbsp; Name="1"});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ang1.Add(new EmployeeModel() { Name = "2" });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; #endregion&nbsp; &nbsp; &nbsp; &nbsp; #region Public Properties&nbsp; &nbsp; &nbsp; &nbsp; public IList<EmployeeModel> Ang1&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ang1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ang1 = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OnPropertyChanged(nameof(Ang1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public EmployeeModel SelectedEmployee&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _selectedEmployee;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _selectedEmployee = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NumeText = value.Name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OnPropertyChanged(nameof(SelectedEmployee));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public string NumeText&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.numeText;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.numeText = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OnPropertyChanged(nameof(NumeText));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; #endregion&nbsp; &nbsp; &nbsp; &nbsp; #region Public Methods&nbsp; &nbsp; &nbsp; &nbsp; public event PropertyChangedEventHandler PropertyChanged;&nbsp; &nbsp; &nbsp; &nbsp; #endregion&nbsp; &nbsp; }模型:&nbsp;public class EmployeeModel&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; }我使用 INotifyPropertyChanged 来绑定通知。让我知道这是否有帮助
打开App,查看更多内容
随时随地看视频慕课网APP