WPF MVVM ComboBox SelectedItem 或 SelectedValue 不起作用 [英] WPF MVVM ComboBox SelectedItem or SelectedValue not working

查看:53
本文介绍了WPF MVVM ComboBox SelectedItem 或 SelectedValue 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新

经过一番调查.问题似乎是 SelectedValue/SelectedItem 在 Item 源完成加载之前发生.如果我坐在断点处等待几秒钟,它会按预期工作.不知道我将如何解决这个问题.

After a bit of investigating. What seems to be the issue is that the SelectedValue/SelectedItem is occurring before the Item source is finished loading. If I sit in a break point and wait a few seconds it works as expected. Don't know how I'm going to get around this one.

结束更新

我有一个应用程序在 WPF 中使用 MVVM 和 ComboBox.下面是 ViewModel 示例.我遇到的问题是,当我们离开页面并迁移回时,组合框没有选择当前选择的值.

I have an application using in WPF using MVVM with a ComboBox. Below is the ViewModel Example. The issue I'm having is when we leave our page and migrate back the ComboBox is not selecting the current Value that is selected.

查看模型

public class MyViewModel
{
     private MyObject _selectedObject;
     private Collection<Object2> _objects;
     private IModel _model;

     public MyViewModel(IModel model)
    {
         _model = model;
         _objects = _model.GetObjects();
    }

    public Collection<MyObject> Objects
    {
         get
         {
              return _objects;
         }
         private set
         {
              _objects = value;
         }
     }

     public MyObject SelectedObject
     {
          get
          {
              return _selectedObject;
          }
          set
          {
               _selectedObject = value;
          }
      }
 }

为了这个例子,假设 MyObject 有两个属性(Text 和 Id).我的 ComboBox XAML 看起来像这样.

For the sake of this example lets say MyObject has two properties (Text and Id). My XAML for the ComboBox looks like this.

XAML

<ComboBox Name="MyComboBox" Height="23"  Width="auto" 
    SelectedItem="{Binding Path=SelectedObject,Mode=TwoWay}" 
    ItemsSource="{Binding Objects}"
    DisplayMemberPath="Text"
    SelectedValuePath="Id">

当我回到页面并重新组合对象时,无论我以哪种方式配置它,ComboBox 都不会选择该值.不过,该对象通过属性中的 get 返回正确的对象.

No matter which way I configure this when I come back to the page and the object is reassembled the ComboBox will not select the value. The object is returning the correct object via the get in the property though.

我不确定这是否只是 ComboBox 和 MVVM 模式工作方式的问题.我们正在做的文本框绑定工作正常.

I'm not sure if this is just an issue with the way the ComboBox and MVVM pattern works. The text box binding we are doing works correctly.

推荐答案

您是否尝试过在视图模型中实现 INotifyPropertyChanged,然后在 PropertyChanged 事件>SelectedItem 设置了吗?

Have you tried implementing INotifyPropertyChanged in your viewmodel, and then raise the PropertyChanged event when the SelectedItem gets set?

如果这本身不能解决它,那么您将能够在导航回页面时自己手动引发 PropertyChanged 事件,这应该足以让 WPF 重新绘制自己并显示正确的所选项目.

If this in itself doesn't fix it, then you will be able to manually raise the PropertyChanged event yourself when navigating back to the page, and that should be enough to get WPF to redraw itself and show the correct selected item.

这篇关于WPF MVVM ComboBox SelectedItem 或 SelectedValue 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆