通过MVVM数据绑定(从组合框)设置模型的FK

时间:2018-12-26 14:59:34

标签: wpf entity-framework mvvm data-binding foreign-keys

这是我的第一个WPF MVVM EF项目,虽然经验非常粗糙,但我怀疑我是否会回到这些方面,但我打算完成它。我有一个视图,您可以在其中编辑Hardware模型的属性。对于“简单”属性(例如字符串,整数,DateTime等),它工作得很好。但是由于某些原因,我无法使其与该模型具有的一些FK属性一起使用。
这是view-viewModel-model代码:

<UserControl x:Class="WPFapp.Views.HardwareManipulationWindowView">
    <UserControl.Resources>
        <DataTemplate DataType="{x:Type localVM:HardwareManipulationViewModel}">
            <local:HardwareManipulationWindowView />
        </DataTemplate>
    </UserControl.Resources>
    <ComboBox SelectedValue="{Binding Hardware.CurrentlyBeingUsedByProgram.GUID, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
              ItemsSource="{Binding ProgramsList}" SelectedValuePath="GUID">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</UserControl>

internal class HardwareManipulationViewModel : NotificationObject
{
    public HardwareManipulationViewModel(Hardware hardware, ObservableCollection<Program> programsList)
    {
        Hardware = hardware;
        ProgramsList = programsList;
    }

    public ObservableCollection<Program> ProgramsList { get; set; }

    public Hardware Hardware { get; }

    internal void WriteChangesInto(Hardware selectedItem)
    {
        selectedItem.Type = Hardware.Type;
        selectedItem.Label = Hardware.Label;
        selectedItem.Description = Hardware.Description;
        selectedItem.Remarks = Hardware.Remarks;
        selectedItem.CurrentLocation = Hardware.CurrentLocation;
        selectedItem.CurrentStatus = Hardware.CurrentStatus;
        //all of the above work just fine, but these 2 FKs below don't work at all
        selectedItem.CurrentlyBeingCarriedByPerson = Hardware.CurrentlyBeingCarriedByPerson; 
        selectedItem.CurrentlyBeingUsedByProgram = Hardware.CurrentlyBeingUsedByProgram;
    }
}

public class Hardware : NotificationObject
{
    protected Hardware()
    {
        GUID = Guid.NewGuid();
    }

    Guid _guid;
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid GUID { get { return _guid; } private set { _guid = value; OnPropertyChanged(); } }

    string _label;
    public string Label { get { return _label; } set { _label = value; OnPropertyChanged(); } }
    string _description;
    public string Description { get { return _description; } set { _description = value; OnPropertyChanged(); } }
    string _remarks;
    public string Remarks { get { return _remarks; } set { _remarks = value; OnPropertyChanged(); } }

    Program _currentlyBeingUsedByProgram;
    public Program CurrentlyBeingUsedByProgram { get { return _currentlyBeingUsedByProgram; } set { _currentlyBeingUsedByProgram = value; OnPropertyChanged(); } }
}

我显然已经省略了很多噪音代码。对于任何想知道NotificationObject是基本INotifyPropertyChanged实现的人。现在,以上所有内容都在此单个方法中处理:

private void InvokeEditHardwareDialog()
{
    HardwareManipulationViewModel viewModel = new HardwareManipulationViewModel(SelectedItem.Clone(), new ObservableCollection<Program>(_dbContext.EagerLoad<Program>()));
    var window = WindowService.CreateWindowHostingViewModel(viewModel, true);
    window.ShowDialog();
    if (viewModel.DialogResult.GetValueOrDefault())
    {
        viewModel.WriteChangesInto(SelectedItem);
        _dbContext.Update(SelectedItem);
    }
}

现在,问题是:当调试器进入该WriteChangesInto方法时,我在其中插入的注释行上方的所有props的新值都使用该视图进行了更改,但是对于最后2个(尽管组合框值已正确加载,但外键)属性没有任何反应。 Hardware.CurrentlyBeingUsedByProgram包含其开头的任何值。我在这里做错了什么?据我所知,这应该可以正常工作。

1 个答案:

答案 0 :(得分:0)

该问题最有可能是EF与WPF通信之间的问题。我最终使用了一种解决方法,其中涉及使用其他属性来存储FK值:

private Program _hardwareProgram;
public Program HardwareProgram { get { return _hardwareProgram; } set { _hardwareProgram = value; OnPropertyChanged(); } }

和XAML:

<ComboBox SelectedValue="{Binding HardwareProgram}"
                          ItemsSource="{Binding ProgramsList}"  IsSynchronizedWithCurrentItem="True">

然后在您的ctor中,您只需读取该值_hardwareProgram = hardware.CurrentlyBeingUsedByProgram;,您就可以开始使用了。