我想在組合框中顯示'用戶組',並將選定的用戶組鍵綁定到我的視圖模型中的變量。我正在使用MVVM範例,我知道它非常接近工作,但我無法看到問題出在哪裏。用戶組在登錄後通過網絡通話動態填充。我知道綁定正在工作,因爲如果我刪除了xaml中的DisplayMemberPath屬性,我看到了這些組。使用MVVM範例填充與c#的組合框使用MVVM範例填充組合框
班裏用戶組
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')
我已經看了看周圍,但我可以似乎無法理解錯誤信息。
順便說一句,你應該讓你的'UserGroup'類實現'INotifyPropertyChanged'或使其不可變。 – Aron