开发者

comboboxedit binding selected value to field with another type of objects in itemssource

I am using wpf and MVVM pattern. I need to bind my comboboxedit ItemsSource to collection of User class. It contains Employee field, which contains string FullName field. I need to bind selected FullName value to string Field of another object in my ViewModel (Document->UserFullName). Ho开发者_Go百科w can I do this.


If I understand you correctly, you have a ComboBox bound to a List of User-instances. The User class has a property of type Employee and the Employee class has a property called FullName of type string. The viewmodel also has a property of type Document and the Document class has a property called UserFullName of type string. When you select a value (a user) in the ComboBox you want to set the value of FullName (User.Employee.FullName) to the UserFullName property of Document (Document.UserFullName).

Correct?

If that is the only thing you want to do, maybe the easisest solution would be to not bind the ComboBox to a collection of User-instances but to a collection of strings that is the FullName of those users (from Employee). That collection wouldn't be to hard to create just by iterating through your list of users. If you bind the ComboBox to a collection of strings then you should be able to just bind the SelectedValue of the ComboBox directly to the UserFullName of the Document (Document.UserFullName).

Another solution would be to have a property "SelectedUser" of type User in your viewmodel and bind the SelectedValue of the ComboBox to this. Whenever the value of this change you also set the value of Document.UserFullName, like this:

private User _selectedUser;
public User SelectedUser
{
    get
    {
        _return _selectedUser;
    }
    set
    {
        if (value != _selectedUser)
        {
            _selectedUser = value;
            Document.UserFullName = _selectedUser.Employee.FullName;
            OnPropertyChanged("SelectedUser");
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜