Skip to main content
 首页 » 编程设计

c#之使用 MVVM 范例用 c# 填充组合框

2024年02月05日18emanlee

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

我的类(class)用户组

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') 

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

请您参考如下方法:

你必须这样做:

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

现在再试一次:)

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