1

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.

CJ_
  • 69
  • 9

1 Answers1

2

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.

dymanoid
  • 14,771
  • 4
  • 36
  • 64
  • thanks for your reply. the User class has following properties, public int Id { get; set; } public string UserName { get; set; } public int GroupId { get; set; } public string DeviceMacAddress { get; set; } how if i just want to bind the selecteditem.UserName ? – CJ_ Feb 13 '15 at 20:40
  • solved. i need to change the type of SelectedItem as UserModel, rather than string. – CJ_ Feb 13 '15 at 20:51