查看未显示更新的属性

时间:2017-10-31 01:18:41

标签: c# wpf mvvm

我是MVVM的新手,并且遇到了简单的文本绑定问题。以下是我的观点:

<UserControl x:Class="Project.ViewModels.SupportArchiveDialog"
         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:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
         mc:Ignorable="d" 
         d:DesignHeight="100" d:DesignWidth="300">
<Grid Margin="16">
    <StackPanel Orientation="Vertical">
        <TextBlock Text="{Binding SupportArchivePath}" Margin="0 5 0 0" FontWeight="Bold" HorizontalAlignment="Center"></TextBlock>
        <Button HorizontalAlignment="Center" HorizontalContentAlignment="Center" IsCancel="True" Margin="0 10 0 0" Style="{DynamicResource MaterialDesignFlatButton}"
                Command="{x:Static wpf:DialogHost.CloseDialogCommand}">
            OK
        </Button>
    </StackPanel>
</Grid>

和我的viewmodel:

using System;
using System.ComponentModel;
using Project.ViewModels;
using MaterialDesignThemes.Wpf;

namespace Project
{
    public class DialogViewModel : INotifyPropertyChanged
    {
        private string _supportArchivePath;

    public string SupportArchivePath
    {
        get { return _supportArchivePath; }
        set
        {
            this.MutateVerbose(ref _supportArchivePath, value, RaisePropertyChanged());
        }
    }

    public async void ExecuteRunDialog(object o)
    {

        var view = new SupportArchiveDialog
        {
            DataContext = new DialogViewModel()               
        };

        //show the dialog
        var result = await DialogHost.Show(view, "RootDialog", ClosingEventHandler);

    }

    private void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
    {

    }

    public event PropertyChangedEventHandler PropertyChanged;

    private Action<PropertyChangedEventArgs> RaisePropertyChanged()
    {
        return args => PropertyChanged?.Invoke(this, args);
    }
}

}

最后我的实现(这个代码块在我的mainwindow后面的代码中运行):

        DialogViewModel dvm = new DialogViewModel();
        dvm.SupportArchivePath = archivePath;
        dvm.ExecuteRunDialog(null);

当我按上面设置属性值时,我希望绑定到属性“SupportArchivePath”的视图中的文本会更新。但是,当我运行项目时,textblock为空。对我缺少什么的想法?

1 个答案:

答案 0 :(得分:0)

事实证明我在演示之前正在实例化我的viewmodel的新实例 - 在我已经设置了属性之后,这就是为什么我从未看到过属性更改的原因。解决方法是改变这个:

    DialogViewModel dvm = new DialogViewModel();
    dvm.SupportArchivePath = archivePath;
    dvm.ExecuteRunDialog(null);

和这个

   public async void ExecuteRunDialog(object o)
   {

    var view = new SupportArchiveDialog
    {
        DataContext = new DialogViewModel()               
    };

    //show the dialog
    var result = await DialogHost.Show(view, "RootDialog", ClosingEventHandler);

   }

到这个

DialogViewModel dvm = new DialogViewModel();
dvm.SupportArchivePath = archivePath;
dvm.ExecuteRunDialog(dvm);

和这个

public async void ExecuteRunDialog(object o)
{
  var view = new SupportArchiveDialog
  {
      DataContext = o              
  };

//show the dialog
var result = await DialogHost.Show(view, "RootDialog", ClosingEventHandler);

}