Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从视图中为ViewModel中的所有属性调用OnChangedProperty?_C#_Wpf_Inotifypropertychanged - Fatal编程技术网

C# 如何从视图中为ViewModel中的所有属性调用OnChangedProperty?

C# 如何从视图中为ViewModel中的所有属性调用OnChangedProperty?,c#,wpf,inotifypropertychanged,C#,Wpf,Inotifypropertychanged,我的Page1.xaml.cs(代码隐藏)中有一个事件,需要更改ViewModel中的所有属性 下面是一个示例:(Page1.xaml.cs) 我怎样才能做到这一点 编辑 我有一个显示.ppt的WebBrowser控件。触发此事件时,我想更新ViewModel中的所有属性: xaml.cs: private void powerPointBrowser1_LoadCompleted(object sender, NavigationEventArgs e) { //...

我的Page1.xaml.cs(代码隐藏)中有一个事件,需要更改ViewModel中的所有属性

下面是一个示例:(Page1.xaml.cs)

我怎样才能做到这一点

编辑

我有一个显示.ppt的WebBrowser控件。触发此事件时,我想更新ViewModel中的所有属性:

xaml.cs:

private void powerPointBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
    {
        //...
        oPPApplication.SlideShowNextSlide += ppApp_SlideShowNextSlide; //Event that gets triggered when i change the Slide in my WebBrowser-Control

    }

private void ppApp_SlideShowNextSlide(PPt.SlideShowWindow Wn)
    {
          // here i dont know how to get access to my Properties in my VM (i want to call OnChangedProperty(//for all properties in my VM))
    }
通常情况下,视图(包括代码隐藏)没有责任通知ViewModel的属性进行更新,反之亦然。但是,我看到,在您的情况下,在处理某些事件时,您希望做某些事情(在本例中,检索每个属性的最新值),因此,这里是:一些您需要的解决方案

在VM中,定义一个激发PropertyChanged到所有属性的方法:

public void UpdateAllProperties()
{
    // Call OnPropertyChanged to all of your properties
    OnPropertyChanged(); // etc. 
}
然后,在视图的代码隐藏中,您需要的只是调用该方法:

// every View has a ViewModel that is bound to be View's DataContext. So cast it, and call the public method we defined earlier.
((MyViewModel)DataContext).UpdateAllProperties();
不幸的是,这种方法对于MVVM风格来说不是很优雅。我建议您将此方法/事件处理程序设置为可绑定的
ICommand
。因此,您不需要在后面编写任何代码,例如:在VM中定义
ICommand

public ICommand UpdateAllPropertiesCommand {get; private set;}
   = new Prism.Commands.DelegateCommand(UpdateAllProperties);
// You can switch the UpdateAllProperties method to private instead.
// Then remove any code behinds you had.
然后,在您的视图(xaml)中,您可以将
ICommand
绑定到特定控件的事件触发器

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

<!--In one of the controls-->
<i:Interaction.Triggers>
   <i:EventTrigger EventName="Loaded">
      <i:InvokeCommandAction Command="{Binding UpdateAllPropertiesCommand , Mode=OneTime}"/>
   </i:EventTrigger>
</i:Interaction.Triggers>
xmlns:i=”http://schemas.microsoft.com/expression/2010/interactivity"
在这里,处理加载的事件时将自动调用该命令。

通常情况下,视图(包括代码隐藏)没有责任通知ViewModel的属性进行更新,反之亦然。但是,我看到,在您的情况下,在处理某些事件时,您希望做某些事情(在本例中,检索每个属性的最新值),因此,这里是:一些您需要的解决方案

在VM中,定义一个激发PropertyChanged到所有属性的方法:

public void UpdateAllProperties()
{
    // Call OnPropertyChanged to all of your properties
    OnPropertyChanged(); // etc. 
}
然后,在视图的代码隐藏中,您需要的只是调用该方法:

// every View has a ViewModel that is bound to be View's DataContext. So cast it, and call the public method we defined earlier.
((MyViewModel)DataContext).UpdateAllProperties();
不幸的是,这种方法对于MVVM风格来说不是很优雅。我建议您将此方法/事件处理程序设置为可绑定的
ICommand
。因此,您不需要在后面编写任何代码,例如:在VM中定义
ICommand

public ICommand UpdateAllPropertiesCommand {get; private set;}
   = new Prism.Commands.DelegateCommand(UpdateAllProperties);
// You can switch the UpdateAllProperties method to private instead.
// Then remove any code behinds you had.
然后,在您的视图(xaml)中,您可以将
ICommand
绑定到特定控件的事件触发器

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

<!--In one of the controls-->
<i:Interaction.Triggers>
   <i:EventTrigger EventName="Loaded">
      <i:InvokeCommandAction Command="{Binding UpdateAllPropertiesCommand , Mode=OneTime}"/>
   </i:EventTrigger>
</i:Interaction.Triggers>
xmlns:i=”http://schemas.microsoft.com/expression/2010/interactivity"

在这里,处理加载的事件时将自动调用该命令。

使用null或空属性名触发视图模型的PropertyChanged事件。是的,我知道我必须使用string.empty^^^,但我不知道如何在我的代码中触发PropertyChanged来获取VM中的属性。我们也不知道,因为您没有向我们展示视图模型的相关部分。我会说,向调用OnPropertyChanged的视图模型添加一个公共方法。只引发datacontextchanged怎么样?对我来说,这似乎是个好消息。您实际想要实现什么?使用null或空属性名触发视图模型的PropertyChanged事件。是的,我知道我必须使用string.empty^^^来执行此操作,但我不知道如何在我的代码中触发PropertyChanged以获取VM中的属性。我们也不知道,因为您没有向我们展示视图模型的相关部分。我会说,向调用OnPropertyChanged的视图模型添加一个公共方法。只引发datacontextchanged怎么样?对我来说,这似乎是个好消息。你到底想要实现什么?