[英]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
,它由一个带有多个UIElement
的Grid
组成。 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 的方法:
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 .wpf [Name of the property] property
, look for the docs.microsoft.com/... link. Google wpf [Name of the property] property
,查找docs.microsoft.com/...链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.