WPF MVVM ComboBox SelectedItem或SelectedValue不起作用

编程入门 行业动态 更新时间:2024-10-27 03:30:54
本文介绍了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不起作用

本文发布于:2023-11-10 06:03:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574547.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   MVVM   WPF   ComboBox   SelectedValue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!