【问题标题】:Populating a combobox with c# using MVVM paradigm使用 MVVM 范例用 c# 填充组合框
【发布时间】:2015-10-05 04:56:26
【问题描述】:

我想在组合框中显示“用户组”,并将选定的用户组键绑定到我的视图模型中的变量。我正在使用 MVVM 范例,我知道它非常接近工作,但我看不出问题出在哪里。用户组是在通过网络调用登录后动态填充的。我知道绑定正在工作,因为如果我删除 xaml 中的 DisplayMemberPath 属性,我会看到组。

我的班级用户组

public class UserGroup
{

    public long ugp_id;
    public String groupName;
    public float ugp_credits;
    public String logo;

    public UserGroup()
    {
    }
}

我有一个 xaml

 ....
 <ComboBox  Grid.Row="0" Grid.Column="2" Height="30" VerticalAlignment="Top" Margin="0,4,0,0"
        ItemsSource="{Binding userGroupCollection, Mode=OneWay}"
        DisplayMemberPath="groupName"
        SelectedValue="{Binding SelectedUgpId}"
        SelectedValuePath="ugp_id" >

在我的 ViewModel 类中,我设置了一个可观察的集合:

  public ObservableCollection<UserGroup> _userGroupCollection;
  public ObservableCollection<UserGroup> userGroupCollection
    {
        get
        {
            return _userGroupCollection;
        }
        set
        {
            if (_userGroupCollection != value)
            {
                this._userGroupCollection = value;
                DynamicOnPropertyChanged();
            }
        }
    }

在网络调用之后,这个数据看起来和我期望的一样......

但是当我在查看页面时查看输出窗口时,我看到组合框条目为空白(但显示了正确的数字)并且出现此错误:

'i3SoftClient.vshost.exe' (CLR v4.0.30319: i3SoftClient.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemCore\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemCore.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
System.Windows.Data Error: 40 : BindingExpression path error: 'ugp_id' property not found on 'object' ''UserGroup' (HashCode=29245900)'. BindingExpression:Path=ugp_id; DataItem='UserGroup' (HashCode=29245900); target element is 'ComboBox' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Error: 40 : BindingExpression path error: 'groupName' property not found on 'object' ''UserGroup' (HashCode=29245900)'. BindingExpression:Path=groupName; DataItem='UserGroup' (HashCode=29245900); target element is 'ComboBox' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Error: 40 : BindingExpression path error: 'ugp_id' property not found on 'object' ''UserGroup' (HashCode=29245900)'. BindingExpression:Path=ugp_id; DataItem='UserGroup' (HashCode=29245900); target element is 'ComboBox' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Error: 40 : BindingExpression path error: 'ugp_id' property not found on 'object' ''UserGroup' (HashCode=29245900)'. BindingExpression:Path=ugp_id; DataItem='UserGroup' (HashCode=29245900); target element is 'ComboBox' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Error: 40 : BindingExpression path error: 'ugp_id' property not found on 'object' ''UserGroup' (HashCode=6695955)'. BindingExpression:Path=ugp_id; DataItem='UserGroup' (HashCode=6695955); target element is 'ComboBox' (Name=''); target property is 'NoTarget' (type 'Object')

我环顾四周,但似乎无法理解错误消息。

【问题讨论】:

    标签: c# wpf xaml mvvm combobox


    【解决方案1】:

    你必须这样做:

    public long ugp_id { get; set; }
    public String groupName { get; set; }
    

    现在再试一次:)

    错误在于那些 wernt 属性,那些仅仅是变量 :) 要让 Binding 对任何成员起作用,您应该像上面一样声明 :)

    【讨论】:

    • 顺便说一句,您应该让您的 UserGroup 类实现 INotifyPropertyChanged 或使其不可变。
    【解决方案2】:

    您不能使用公共字段进行绑定。你必须使用属性

    public class UserGroup
    {
    
        public long ugp_id { get; set; };
        public String groupName { get; set; };
        public float ugp_credits { get; set; };
        public String logo { get; set; };
    
        public UserGroup()
        {
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-25
      • 2013-05-29
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      相关资源
      最近更新 更多