Reputation: 3974
I want to display 'user groups' in a combobox, and bind the selected usergroup key to a variable in my view model. I am using the MVVM paradigm, and I know it is very close to working but i just can't see where the issue is. usergroups is is populated dynamically after a login via a web call. I know the binding is working because if i remove the DisplayMemberPath attribute in the xaml, i see the groups.
my class UserGroup
public class UserGroup
{
public long ugp_id;
public String groupName;
public float ugp_credits;
public String logo;
public UserGroup()
{
}
}
i have a 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" >
in my ViewModel Class i set up an observable collection :
public ObservableCollection<UserGroup> _userGroupCollection;
public ObservableCollection<UserGroup> userGroupCollection
{
get
{
return _userGroupCollection;
}
set
{
if (_userGroupCollection != value)
{
this._userGroupCollection = value;
DynamicOnPropertyChanged();
}
}
}
after the web call this data looks as I expect it to...
but when I look at the output window when the page is viewed i see the combobox entries are blank (but the correct number are shown) and this error:
'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')
I have looked around, but I just can't seem to understand the error message.
Upvotes: 1
Views: 94
Reputation: 3439
You can't use public fields for a binding. You must use properties
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()
{
}
}
Upvotes: 1
Reputation: 844
You have to do it like this :
public long ugp_id { get; set; }
public String groupName { get; set; }
Try again now :)
What the error was that those wernt properties, those were mere variables :) For Binding to work on any member, you should declare as above :)
Upvotes: 3