MVVMLight -当属性改变时UI不更新
本文关键字:UI 更新 改变 属性 MVVMLight | 更新日期: 2023-09-27 17:52:34
我正在使用c#和MVVMLight开发一个小型数据驱动应用程序,当我试图添加/编辑新项目时,我遇到了一个问题。
基本上,我可以在构造函数中将SelectedMember属性设置为数据库中已经存在的Member对象,没有问题:
public MemberDetailViewModel()
{
_context = new DatabaseEntities();
GetMembers(); // This just loads the Member objects into an ObservableCollection.
SelectedMember = _members.FirstOrDefault(); // UI updates as expected.
}
SelectedMember的实现如下:
public Member SelectedMember
{
get { return _selectedMember; }
set { Set(ref _selectedMember, value); }
}
现在这很好,因为当我编辑对象时,所有的数据验证工作。
但是,当我使用下面的代码添加一个新对象时:
private void NewMember()
{
var member = new Member();
SelectedMember = member; // UI doesn't update, is still on previous object.
}
UI没有更新,并且SelectedMember中的Member对象似乎与构造函数中加载的成员对象相同。
另外,当我从构造函数中加载一个空对象到SelectedMember属性时,应用程序似乎不知道它的存在,并且当我编辑控件中的值时没有数据验证例程:
public MemberDetailViewModel()
{
_context = new DatabaseEntities();
SelectedMember = new Member(); // Binding appears to fail, data validation code not firing when control values changed.
}
我做错了什么?
编辑:当视图模型被加载的时候我看到了这个
System.Windows.Data Error: 40 : BindingExpression path error: 'Centres' property not found on 'object' ''Member_B31A44ECE333F4E7E07BE25FA5BDC79874BAE6C64589F9765ACA373D89BBBD6B' (HashCode=45485186)'. BindingExpression:Path=Centres; DataItem='Member_B31A44ECE333F4E7E07BE25FA5BDC79874BAE6C64589F9765ACA373D89BBBD6B' (HashCode=45485186); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
System.Windows.Data Warning: 40 : BindingExpression path error: 'SelectedMember' property not found on 'object' ''Member_B31A44ECE333F4E7E07BE25FA5BDC79874BAE6C64589F9765ACA373D89BBBD6B' (HashCode=45485186)'. BindingExpression:Path=SelectedMember.LocalCentre; DataItem='Member_B31A44ECE333F4E7E07BE25FA5BDC79874BAE6C64589F9765ACA373D89BBBD6B' (HashCode=45485186); target element is 'ComboBox' (Name=''); target property is 'SelectedValue' (type 'Object')
我觉得我做了什么蠢事…但是什么?
编辑2:上面的错误是固定的,但UI仍然不更新,当SelectedMember已经改变,即使显式调用RaisePropertyChanged。
Edit3: 希望这是你需要的XAML:
<UserControl x:Class="Project.Views.MemberDetailView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:models="clr-namespace:Project.Client.Models"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="800">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Assets/ResourceDictionaries/DataEntryScreens.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<WrapPanel>
<Expander Header="Personal Information">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Vertical">
<TextBlock Text="Forename:" />
<TextBox Text="{Binding SelectedMember.Forename, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
<!-- Other controls omitted. -->
</Expander>
</WrapPanel>
</UserControl>
<!--MemberDetailView.xaml.cs-->
public partial class MemberDetailView : UserControl
{
public MemberDetailView()
{
InitializeComponent();
this.DataContext = new MemberDetailViewModel();
}
}
我已经解决了这个…不知何故…
我不确定我做了什么,但显然在视图的构造函数中设置了一个ViewModel,把一切都搞砸了。
所以从这个开始:
public MemberDetailView()
{
InitializeComponent();
this.DataContext = new MemberDetailViewModel() { Header = "Members" };
}
:
public MemberDetailView()
{
InitializeComponent();
//this.DataContext = new MemberDetailViewModel() { Header = "Members" };
}
已修复问题