简体   繁体   English

识别从其继承属性的基础 class

[英]Identify the base class from which a property is inherited

In order to clean up my code by using pattern matching, i need to find the common base class from which a certain common property is inherited from.为了通过使用模式匹配来清理我的代码,我需要找到继承某个公共属性的公共基础 class。

In the following exemple, the sender is a wpf DataTemplate consisting of a Grid with several UIElement on it.在以下示例中, sender是一个 wpf DataTemplate ,它由一个带有多个UIElementGrid组成。 I want to initiate a drag event with the source of the data template, a Vehicle that can be accessed from any object's DataContext property.我想用数据模板的源启动一个拖动事件,一个可以从任何对象的DataContext属性访问的Vehicle

I need to find the common base class from which my UIElements inherit the DataContext property in order to write clean code:我需要找到我的UIElements从中继承DataContext属性的公共基础 class 以便编写干净的代码:

private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
   if (e.LeftButton == MouseButtonState.Pressed)
   {
      if (e.Source is CommonBaseClassFromWhichDataContextPropertyIsInherited obj)
      {
         var vehicle = obj.DataContext;
         DataObject data = new DataObject();
         data.SetData(typeof(Vehicle), vehicle);
         DragDrop.DoDragDrop(this, data, DragDropEffects.Move);
      }
   }
}

@thatguy answered for my exemple: FrameWorkElement is the class I'm looking for. @thatguy 以我为例回答:FrameWorkElement 是我正在寻找的 class。

But my question is more about the method to determine the base class : Do you browse metadata?但我的问题更多的是关于确定基础 class 的方法:你浏览元数据吗? Is there an intellisense function?是否有智能感知 function?

The FrameworkElement type defines the DataContext property, so you can match it. FrameworkElement类型定义了DataContext属性,因此您可以匹配它。

if (e.Source is FrameworkElement frameworkElement)
{
   var vehicle = frameworkElement.DataContext;
   // ...
}

Methods to determine the base class:确定碱基 class 的方法:

  • Use IntelliSense to browse the metadata, eg click on DataContext in XAML or C# code and hit F12 or right-click and select Go to definition . Use IntelliSense to browse the metadata, eg click on DataContext in XAML or C# code and hit F12 or right-click and select Go to definition .
  • Google wpf [Name of the property] property , look for the docs.microsoft.com/... link. Google wpf [Name of the property] property ,查找docs.microsoft.com/...链接。
  • Remember a long forgotten training where it was mentioned or one of the countless blogs or other sites where you searched for something completely different and learned it by accident.记住一个被遗忘已久的培训,或者在无数博客或其他网站中,您搜索了完全不同的东西并偶然学到了它。
  • Ask a question on StackOverflow , get an answer (warning, recursive).在 StackOverflow 上提问,得到答案(警告,递归)。
  • Meet your future self who knows it already through time travel, but be careful to not break the space-time continuum, been there, done that, hard to fix, even harder to explain.通过时间旅行认识你未来的自己,但要小心不要打破时空连续体,去过那里,做过那个,很难修复,更难解释。
  • Join the dark side and use unlimited power, alternatively become a Jedi and meditate.加入黑暗面并使用无限的力量,或者成为绝地并冥想。

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

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