开发者

Data binding a WPF ComboBox from a ViewModel - can't change selected item

I'll get this out of the way right off the bat... my base view model class is implementing INotifyPropertyChanged. Here's the scenario:

I have a single view with a single view model. The view is a master/detail with the master being a list box of Game objects that I'm populating without issue. When a Game object is selected in the master list box, I want to populate some details in various controls. The control that's causing me problems is a combo box.

Now, the combobox is being populated using a collection of Team objects. Each Game object has a "Team" object and once the combobox is populated, I want to select the appropriate Team object in the combobox that the Game object specifies.

Now, I know this is working to some degree because if I do the same binding to a textbox, the right information appears (I can get the bound Team object to appear in the 开发者_StackOverflow社区textbox, but I can't get it to select from the list).

I'm seriously lost, been working on this for a few hours now. Can anyone assist?

Edit: I have a feeling this has something to do with the object references. But wouldn't SelectedValue still work?


ViewModel:

    public ObservableCollection<Team> Teams
    {
        get { return this.teams; }
        set
        {
            this.teams = value;
            OnPorpertyChanged("Teams");
        }
    }

    public Team SelectedTeam
    {
        get { return this.selectedTeam; }
        set
        {
            this.selectedTeam = value;
            OnPorpertyChanged("SelectedTeam");
        }
    }

    private ObservableCollection<Team> teams;
    private Team selectedTeam;

Team Class:

    public class Team
    {
        public string Name { get; set; }    
    }

View:

        <ComboBox DisplayMemberPath="Name"
              ItemsSource="{Binding Teams}" 
              SelectedItem="{Binding Mode=OneWayToSource, Path=SelectedTeam}" />
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜