![](/img/trans.png)
[英]Getting selected item from a WPF combobox populated by data from an access database
[英]Getting the selected value from a combobox populated from a data base, for comparison, through data binding WPF/ MVVM
我是C#的初學者; 該項目是使用實體框架和視圖模型在Visual Studio(wpf)中完成的。 我從數據庫中僅填充了一個名為“ Employees”的組合框。 我想做的是獲取變量中的選定項目值(或直接將其與數據庫中存儲的名稱進行比較,以獲取與該名稱關聯的所有其他表列;在我的情況下,我有年齡,屬性..etc )
如果我直接進入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 value
names in the database
的names 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;
}
}
您可以使用包含模型的List並綁定到組合框itemsource,還可以將DisplayMemberPath綁定為說“ Name”,這是模型的屬性,並將SelectedItem綁定到此模型。
請查看代碼。 我使用了偽字符串值。 在您的情況下,應該按照您提到的從DB中填充它。 請注意,在SelectedEmployee的設置器中,我正在根據您的要求向NumText賦值。 所選項目更改后,將立即對其進行分配並顯示在XAML文本框中
在您的情況下,您錯誤地將SelectedItem分配給list。 它應該與您綁定的itemsource有關。 您創建了單獨的列表,其中一個用於itemsource,一個用於所選項目。 而且我也看不到代碼中與INotifyPropertyChanged相關的屬性設置器中的任何更改。
XAML:
<Window x:Class="WpfApplication13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication13"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50*"/>
<RowDefinition Height="50*"/>
</Grid.RowDefinitions>
<TextBox x:Name="text_nume" HorizontalAlignment="Center" Height="23" TextWrapping="Wrap" Text="{Binding NumeText}" VerticalAlignment="Center" Width="89" Grid.Row="0" />
<ComboBox x:Name="comboPersonal" Grid.Row="1" Height="30" Width="100" HorizontalAlignment="Center" DisplayMemberPath="Name" VerticalAlignment="Center" Margin="13,60,-62,0" ItemsSource="{Binding Ang1}" SelectedItem="{Binding SelectedEmployee}" />
</Grid>
</Window>
ViewModel:
public class ViewModel : INotifyPropertyChanged
{
#region Constants and Enums
#endregion
#region Private and Protected Member Variables
private IList<EmployeeModel> ang1;
EmployeeModel _selectedEmployee;
private string numeText;
#endregion
#region Private and Protected Methods
private void OnPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
#endregion
#region Constructors
public ViewModel()
{
Ang1 = new List<EmployeeModel>();
Ang1.Add(new EmployeeModel() { Name="1"});
Ang1.Add(new EmployeeModel() { Name = "2" });
}
#endregion
#region Public Properties
public IList<EmployeeModel> Ang1
{
get
{
return ang1;
}
set
{
ang1 = value;
OnPropertyChanged(nameof(Ang1));
}
}
public EmployeeModel SelectedEmployee
{
get
{
return _selectedEmployee;
}
set
{
_selectedEmployee = value;
NumeText = value.Name;
OnPropertyChanged(nameof(SelectedEmployee));
}
}
public string NumeText
{
get
{
return this.numeText;
}
set
{
this.numeText = value;
OnPropertyChanged(nameof(NumeText));
}
}
#endregion
#region Public Methods
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
模型:
public class EmployeeModel
{
public string Name { get; set; }
}
我使用INotifyPropertyChanged進行綁定通知。 讓我知道這是否有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.