简体   繁体   English

视觉工作室 WPF C# | OnPropertyChange | 不清楚的行为

[英]Visual Studio WPF C# | OnPropertyChange | unclear behaviour

I'm working on a plugin that I didn't start developing and there is a behaviour that I don't understand.我正在开发一个我没有开始开发的插件,并且有一种我不理解的行为。

Code:代码:

MAINWINDOW.xaml主窗口.xaml

TABLE1 and TABLE2 structures TABLE1 和 TABLE2 结构

<DataGrid ...>
   ...
   <DataGridTemplateColumn Header="title" Width="Auto">
      <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
            <TextBox Text="{Binding Path=MYPROPERTY, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
         </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
   </DataGridTemplateColumn>
   ...
</DatGrid>

MAINWINDOW.xaml.cs MAINWINDOW.xaml.cs

      private void MYCLASS_Changed(object sender, EventArgs e)
      {
LineY    if (!_isInitialising) { Recalculate(); }
      }

OTHER C# FILE其他 C# 文件

namespace NAME
{
   public class MYCLASS : INotifyPropertyChanged
   {
      ...
      public event PropertyChangedEventHandler PropertyChanged;
      ...
      public double MYPROPERTY
      {
         get {...}
         set {...; OnPropertyChanged("MYPROPERTY");}
      }
      ...
      public void OnPropertyChanged(string name)
      {
         PropertyChangedEventHandler handler = PropertyChanged;
         if (handler != null)
         {
LineX       handler(this, new PropertyChangedEventArgs(name));
         }
      }
   }
}

Behaviour: I have a first table TABLE1 in MAINWINDOW.xaml.行为:我在 MAINWINDOW.xaml 中有第一个表 TABLE1。 When I change MYPROPERTY in TABLE1 it calls the OnPropertyChange (LineX) and then jumps to VOID MYCLASS_CHANGED in MAINWINDOW.xaml.cs (LineY) and I don't undertand why this link happens, is someone able to explain it or tell me what to look when debugging (I'm not an actual programmer)?当我在 TABLE1 中更改 MYPROPERTY 时,它会调用 OnPropertyChange (LineX),然后跳转到 MAINWINDOW.xaml.cs (LineY) 中的 VOID MYCLASS_CHANGED,我不明白为什么会出现此链接,是否有人能够解释它或告诉我该怎么做调试时看看(我不是真正的程序员)?

I have a second table TABLE2 in MAINWINDOW.xaml.我在 MAINWINDOW.xaml 中有第二个表 TABLE2。 When I change MYPROPERTY in TABLE2 it calls the OnPropertyChange but it doesn't jump to the other function. I would like to in order to call some functions inside MAINWINDOW.xaml.cs当我在 TABLE2 中更改 MYPROPERTY 时,它调用 OnPropertyChange 但它不会跳转到另一个 function。我想调用 MAINWINDOW.xaml.cs 中的一些函数

Your code appears to be structured as Model - View - ViewModel pattern (MVVM).您的代码的结构似乎为 Model - View - ViewModel 模式 (MVVM)。

Your ViewModel cannot/should not access the View directly hence the use of Binding this instructs the .NET framework to expect an event trigger in which the new value of the bound parameter is sent - this is the PropertyChangedEvent .您的 ViewModel 不能/不应直接访问 View,因此使用Binding指示 .NET 框架期望发送绑定参数的新值的事件触发器 - 这就是PropertyChangedEvent If declared "OneWay" then this is from the ViewModel to the View, if "BothWays" then entry of data on the View will trigger a change within the ViewModel property.如果声明为“OneWay”,则这是从 ViewModel 到 View,如果声明为“BothWays”,则在 View 上输入数据将触发 ViewModel 属性内的更改。

MainWindow.xaml and MainWindow.xaml.cs jointly make up the View (hence the partial declaration within MainWindow.xaml.cs). MainWindow.xaml 和 MainWindow.xaml.cs 共同构成视图(因此在 MainWindow.xaml.cs 中进行partial声明)。

MainWindow.xaml.cs is what is referred to as the "code behind" file for MainWindow.xaml. MainWindow.xaml.cs 是 MainWindow.xaml 的“代码隐藏”文件。

In MVVM the View displays data that has been presented/retrieved/calculated by the ViewModel which in turn gets its base data structure from the Model.在 MVVM 中,View 显示由 ViewModel 呈现/检索/计算的数据,ViewModel 又从 Model 获取其基本数据结构。

There is some overlap in function, often you will see no Model (all of the data being held within the ViewModel) or some business logic within the code behind file (rather than just within the ViewModel). function 有一些重叠,通常您不会看到 Model(所有数据都保存在 ViewModel 中)或代码隐藏文件中的一些业务逻辑(而不仅仅是在 ViewModel 中)。

It would appear that, in your code, you have the OnPropertyChangedEvent from the ViewModel triggering the MYCLASS_Changed event handler within your code behind file.看起来,在您的代码中,ViewModel 的OnPropertyChangedEvent触发了代码隐藏文件中的MYCLASS_Changed事件处理程序。

I suspect that this is related to your data trigger set by UpdateSourceTrigger=PropertyChanged but can't say for certain with the code that you have posted.我怀疑这与您通过UpdateSourceTrigger=PropertyChanged设置的数据触发器有关,但不能确定您发布的代码。

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

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