如何将 'System.Windows.Controls.TextBlock' 转换为 'System.Windows.Controls.Control' WPF C#
How to convert 'System.Windows.Controls.TextBlock' to 'System.Windows.Controls.Control' WPF C#
据我所知,TextBlock
的基数 class 是 Control
(as said here)。我正在尝试将 System.Windows.Controls.TextBlock
实例转换为 System.Windows.Controls.Control
,如下所示:
Control c1 = TbPosDir;
其中 TbPosDir
是 TextBlock
但我收到以下错误:
Error CS0029 Cannot implicitly convert type
'System.Windows.Controls.TextBlock' to
'System.Windows.Controls.Control' ...
如何进行上述转换?
请注意,我可以毫无问题地将其他 Control
实例(例如 TextBox
转换为 Control
:
var txt = new TextBox();
Control c2 = txt;
TextBlock 派生自 FrameworkElement,而非 Control
public class TextBlock : FrameworkElement, IServiceProvider, IContentHost, IAddChild, IAddChildInternal
是的 - TextBlock
在其继承层次结构中没有任何地方 Control
,即:
Object
DispatcherObject
DependencyObject
Visual
UIElement
FrameworkElement
TextBlock
Control
是另一个 class 直接派生自 FrameworkElement
。大概 TextBlock
不是从 Control
派生的,因为它没有 ControlTemplate
.
所以不,您不能将 TextBlock
用作 Control
...但是您 可能 不需要。您没有显示您尝试使用控件做什么,但在 大多数 情况下,我怀疑您可以使用 FrameworkElement
甚至基本 class例如Visual
.
据我所知,TextBlock
的基数 class 是 Control
(as said here)。我正在尝试将 System.Windows.Controls.TextBlock
实例转换为 System.Windows.Controls.Control
,如下所示:
Control c1 = TbPosDir;
其中 TbPosDir
是 TextBlock
但我收到以下错误:
Error CS0029 Cannot implicitly convert type 'System.Windows.Controls.TextBlock' to 'System.Windows.Controls.Control' ...
如何进行上述转换?
请注意,我可以毫无问题地将其他 Control
实例(例如 TextBox
转换为 Control
:
var txt = new TextBox();
Control c2 = txt;
TextBlock 派生自 FrameworkElement,而非 Control
public class TextBlock : FrameworkElement, IServiceProvider, IContentHost, IAddChild, IAddChildInternal
是的 - TextBlock
在其继承层次结构中没有任何地方 Control
,即:
Object
DispatcherObject
DependencyObject
Visual
UIElement
FrameworkElement
TextBlock
Control
是另一个 class 直接派生自 FrameworkElement
。大概 TextBlock
不是从 Control
派生的,因为它没有 ControlTemplate
.
所以不,您不能将 TextBlock
用作 Control
...但是您 可能 不需要。您没有显示您尝试使用控件做什么,但在 大多数 情况下,我怀疑您可以使用 FrameworkElement
甚至基本 class例如Visual
.