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

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

问题描述

更新

经过一番调查。什么似乎是问题是SelectedValue / SelectedItem正在项目源完成加载之前发生。如果我坐在一个断点,等待几秒钟,它如预期工作。不知道我该如何解决这个问题。

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示例。我遇到的问题是当我们离开我们的页面并迁移回ComboBox没有选择当前选择的值。

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)。我的XAML for ComboBox看起来像这样。

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 在您的viewmodel,然后提高 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天全站免登陆