手动设置组合框项目时,如何使用mvvm中的绑定在wpf中设置组合框默认值

阿伦vk

我在WPF中使用mvvm。我正在设置一个表单来更新用户数据。我有一个组合框可以选择用户性别。我已经在源中手动添加了组合框项目。加载数据以形成所有字段时,其他字段将正确显示。但组合框未显示任何内容。我使用了twoWay绑定,并且我从表单中选择的值都进入了viewModel。我一直在搜索数小时,发现了许多类似的问题,但对我没有任何帮助。我在下面插入我的代码段。请给我一个解决方案。

<ComboBox 
    Grid.Column="2" 
    SelectedItem="{Binding SelectedEmployees.gender, Mode=TwoWay}" 
    SelectedValue="{Binding SelectedEmployees.gender, Mode=TwoWay}"
    >
    <ComboBoxItem Content="Male"/>
    <ComboBoxItem Content="Female"/>
</ComboBox> 

我的viewModel代码如下

class EmployeesModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private iServiceClient serviceClient = new iServiceClient();
    public EmployeesModel()
    {
        this.RefreshEmployees();
    }

    private void RefreshEmployees()
    {
        this.serviceClient.GetAllEmployeesCompleted += (s, e) =>
          {
              this.employees = e.Result;
          };

        this.serviceClient.GetAllEmployeesAsync();
    }

    private IEnumerable<Employee> employees;
    public IEnumerable<Employee> Employees
    {
        get
        {
            return this.employees;
        }
        set
        {
            this.employees = value;
            this.OnPropertyChanged("Employees");
        }
    }

    private Employee selectedEmployees;
    public Employee SelectedEmployees
    {
        get
        {
            return this.selectedEmployees;
        }
        set
        {
            this.selectedEmployees = value;
            this.OnPropertyChanged("SelectedEmployees");
        }
    }

    public void OnPropertyChanged(string PropertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }

}

并且SelectedEmployees类是

public class Employee
{
    [Key]
    public int id { get; set; }
    public DateTime JoiningDate { get; set; }
    public string name { get; set; }
    public string gender { get; set; }
    public string mobile { get; set; }
    public string post { get; set; }
    public string salaryType { get; set; }
    public decimal salary { get; set; }
    public string docname { get; set; }
    public int validit { get; set; }
}
安迪

我怀疑SelectedEmployees.gender不是comboboxitem类型。
直接使用在组合框中创建组合框项目的捷径是一个错误的举动。

当我做:

    xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Window.DataContext>
    <local:MainWIndowViewModel/>
</Window.DataContext>
<Window.Resources>
    <x:Array Type="sys:String" x:Key="Genders">
        <sys:String>Male</sys:String>
        <sys:String>Female</sys:String>
    </x:Array>
</Window.Resources>
<Grid>
    <ComboBox 
     SelectedItem="{Binding gender, Mode=TwoWay}" 
        ItemsSource="{StaticResource Genders}"
        />
</Grid>

在绑定的性别中,我得到的是字符串而不是comboboxitem。您可能想要更多类似的东西。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用MVVM在组合框中设置默认值

如何在tkinter的组合框中设置默认值?

使用 Xaml 设置默认值或组合框(尽管绑定)

如何设置组合框默认值?

将默认值设置为php中的组合框

C#-如何在组合框datagridview中的表中将值设置为默认值?

集会设置组合框的默认值

如何为组合框设置默认值?

使用WPF中的Value从C#设置组合框项目

手动添加时绑定datagrdview组合框

从第一个表值Access中设置表单的组合框默认值

即使在vb.net中设置为下拉列表,组合框也不显示默认值

如何在 JavaScript 中编辑组合框的默认值

如何为orca中的组合框提供默认值?

使用ModelViewViewModel的组合框中的默认值

如何将默认组合框值设置为在属性文件中设置的值?

如何在组合框中设置所选项目并在txtfield中设置等效值

wpf / mvvm-如何制作一个组合框,以设置mvvm中所选项目的特定值

在beforePageLoad事件中设置组合框的值

C#WPF Datagrid-设置数据绑定组合框的默认选定值

如何设置组合框值?

如何为从表/查询中提取值的组合框设置默认值?

如何在angularjs中设置选择框的默认值

如何在指令中设置选择框的默认值

剑道网格中组合框的默认值

使用WPF和Entity Framework设置查找组合框的绑定

C# - 如何通过比较我的 int 值在组合框中设置所选项目?

如何使用MySQL数组中的值设置jQuery函数中文本框的默认值?

从枚举绑定的 wpf 组合框中查找所选项目