我是 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;
}
}
慕妹3242003
相关分类