简体   繁体   English

在不同的类中实现INotifyPropertyChanged

[英]Implement INotifyPropertyChanged in different class

I have 2 ViewModels in my application. 我的应用程序中有2个ViewModel。 The first (FirstPageViewModel) is responsible for the data which gets shown in the TextBoxes in my View. 第一个(FirstPageViewModel)负责在我的View中的TextBox中显示的数据。 The other ViewModel (NavigationViewModel) is responsible for the Navigation between my pages AND to change the values of the TextBlocks: 另一个ViewModel(NavigationViewModel)负责我的页面之间的导航和更改TextBlocks的值:

<StackPanel>
<Button Content="SecondPage" 
        DataContext="{Binding Source={StaticResource NavigationVM}}" /// reference to App.xaml
        Command="{Binding NavigationCommand}" 
        CommandParameter="SecondPage" />
<Grid  DataContext="{Binding Source={StaticResource FirstPageViewModel}}">
  <TextBlock  Text="{Binding helloWorld.Counter, UpdateSourceTrigger=PropertyChanged}"/>
  <TextBlock  Text="{Binding helloWorld.Message, UpdateSourceTrigger=PropertyChanged}"/>
    ...
</Grid>

Now the Navigation works fine. 现在导航工作正常。 But if I try to change the values in the TextBlocks by using the NavigationCommand (=Button Click) in "NavigationViewModel", nothing changes: (TextBlockSetter implements INotifyPropertychanged) 但是,如果我尝试使用“NavigationViewModel”中的NavigationCommand(= Button Click)更改TextBlocks中的值,则没有任何更改:(TextBlockSetter实现INotifyPropertychanged)

public TextBlockSetter _helloWorld;


    public NavigationViewModel()
    {

        _helloWorld = new TextBlockSetter();

    }

    public TextBlockSetter helloWorld
    {
        get
        {
            return _helloWorld;
        }
        set
        {
            _helloWorld = value;
        }
    }
 private void navigationCommand(object destination)
{
  switch (destination.ToString())
  {
    case "SecondPage":
      {

         ... ///Code for page Navigation

        helloWorld.Counter++;
        helloWorld.Message = "done";
        break;
      }
  }
}

"FirstPageViewModel" contains the same implementation and sets the values of the TextBoxes: “FirstPageViewModel”包含相同的实现并设置TextBoxes的值:

static int _roundCounter = 1;
public TextBlockSetter _helloWorld;

    public FirstPageViewModel()
    {
        helloWorld.Counter = _roundCounter;
        _helloWorld = new TextBlockSetter();

    }

    public TextBlockSetter helloWorld
    {
        get
        {
            return _helloWorld;
        }
        set
        {
            _helloWorld = value;
        }
    }

Has someone an idea how to properly implement those changes? 有人知道如何正确实施这些变化吗? My idea was to make a reference in NavigationViewModel to FirstPageViewModel, when the Textboxes should get changed. 我的想法是在NavigationboxModel中引用FirstPageViewModel,当文本框应该被更改时。 But unfortunately none of my ideas worked out well. 但不幸的是,我的想法都没有成功。

I dont fully understand but, what about use just a one viewmodel and define a screen property in that viewmodel.Therefore, you can set that property and then navigate in property changed event. 我不完全理解,但是,只使用一个viewmodel并在该viewmodel中定义一个屏幕属性。因此,您可以设置该属性,然后导航属性更改事件。

public static readonly DependencyProperty ScreenNameProperty =
        DependencyProperty.Register("ScreenName", typeof(String),
        typeof(ContentControl), new FrameworkPropertyMetadata("screen_1", new PropertyChangedCallback(ScreenNameChanged)));

    public String ScreenName
    {
        get { return (String)GetValue(ScreenNameProperty); }
        set { SetValue(ScreenNameProperty, value); }
    }

    private static void ScreenNameChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
    {
        ContentControl originator = dependencyObject as ContentControl;
        if (originator != null)
        {
            // navigate here
        }
    }

bind ScreenName property to a property in your viewmodel.Then change propertt in viewmodel how you want. 将ScreenName属性绑定到viewmodel中的属性。然后在viewmodel中更改属性的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM