我想在组合框中显示“用户组”,并将选定的用户组键绑定到视图模型中的变量。我正在使用MVVM范例,我知道它非常接近工作,但我无法看到问题所在。通过Web调用登录后动态填充usergroups。我知道绑定工作正常,因为如果我删除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')
我环顾四周,但我似乎无法理解错误信息。
答案 0 :(得分:3)
你必须这样做:
public long ugp_id { get; set; }
public String groupName { get; set; }
立即重试:)
那些wernt属性的错误是什么,那些仅仅是变量:) 要使绑定适用于任何成员,您应该声明,如上所述:)
答案 1 :(得分:1)
您不能将公共字段用于绑定。您必须使用属性
10