从 ViewModel 绑定 WPF ComboBox 的数据 - 无法更改所选项目

发布于 2024-12-05 01:27:27 字数 638 浏览 2 评论 0原文

我会立即解决这个问题...我的基本视图模型类正在实现 INotifyPropertyChanged。场景如下:

我有一个带有单个视图模型的视图。该视图是一个主视图/细节视图,其中主视图是一个 Game 对象的列表框,我可以毫无问题地填充这些对象。当在主列表框中选择 Game 对象时,我想在各种控件中填充一些详细信息。给我带来问题的控件是组合框。

现在,使用 Team 对象集合填充组合框。每个 Game 对象都有一个“Team”对象,填充 combobox 后,我想在 中选择适当的 Team 对象Game 对象指定的组合框

现在,我知道这在某种程度上是有效的,因为如果我对 textbox 进行相同的绑定,就会出现正确的信息(我可以让绑定的 Team 对象出现在文本框,但我无法从列表中选择它)。

我严重迷失了,已经为此工作了几个小时。有人可以帮忙吗?

编辑:我有一种感觉这与对象引用有关。但是 SelectedValue 仍然有效吗?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘年祭陌 2024-12-12 01:27:27

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;

团队类别:

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

视图:

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

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}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文