簡體   English   中英

使用MVVM范例使用c#填充組合框

[英]Populating a combobox with c# using MVVM paradigm

我想在組合框中顯示“用戶組”,然后將選定的用戶組鍵綁定到我的視圖模型中的變量。 我正在使用MVVM范例,我知道它非常接近工作,但是我看不出問題出在哪里。 通過網絡調用登錄后,動態填充用戶組。 我知道綁定正在工作,因為如果我在xaml中刪除DisplayMemberPath屬性,則會看到這些組。

我班的UserGroup

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; }

現在再試一次:)

錯誤的是那些無效的屬性,它們僅僅是變量:)對於要在任何成員上進行綁定的綁定,您都應該如上所述聲明 :)

您不能使用公共字段進行綁定。 您必須使用屬性

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()
    {
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM