The View is:
<Controls:SplitButton Margin="217,409.75,56,185" Name="SplitButton1"
Width="384"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Orientation="Vertical"
DisplayMemberPath ="UserName"
SelectedItem="{Binding SelectedUser,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}"
ItemsSource="{Binding Users, Mode=TwoWay}" />
The ViewModel is:
public string SelectedUser
{
get { return selectedUser; }
set
{
selectedUser = value;
RaisePropertyChanged("SelectedUser");
}
}
public ObservableCollection<UserModel> Users
{
get
{
return users;
}
set
{
users = value;
}
}
the Model is:
public class UserModel
{
private int id;
private string userName;
private int groupId;
private string deviceMacAddress;
public int Id { get; set; }
public string UserName { get; set; }
public int GroupId { get; set; }
public string DeviceMacAddress { get; set; }
}
i use the above code in xaml to bind the selectedItem in the splitbutton to ViewModel->property--SelectedUser.
but it does not work. anyone knows why?
SelectedUser is returned as Model name (PresentationLayer.Model.UserModel) instead of UserName prooperty.
Because your binding is OneWay
by your definition.
Set your binding to TwoWay
.
<Controls:SplitButton SelectedItem="{Binding SelectedUser,Mode=TwoWay}"/>
And, there is no need to set the UpdateSourceTrigger=PropertyChanged
in this case, because the UpdateSourceTrigger
is PropertyChanged
by default for the SelectedItem
property.