我正在使用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: 上述错误是固定的,但即使显式调用RaisePropertyChanged,当SelectedMember发生更改时,UI仍然不会更新。
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();
}
}
答案 0 :(得分:0)
我已经解决了......不知怎的......
我不确定我做了什么,但显然在视图的构造函数中设置了一个ViewModel,设法弄乱了所有内容。
所以从这里开始:
public MemberDetailView()
{
InitializeComponent();
this.DataContext = new MemberDetailViewModel() { Header = "Members" };
}
到此:
public MemberDetailView()
{
InitializeComponent();
//this.DataContext = new MemberDetailViewModel() { Header = "Members" };
}
已解决问题。