WPF 自定义控件不能从文本块控件继承



我正在用CustomControls制作一个WPF CustomControlLibrary,它继承了Label、TextBox等标准控件。当我试图制作另一个从TextBlock继承的CustomControl时,我会遇到奇怪的错误。CustomControl似乎无法从TextBlock继承。

但为什么呢?

提前感谢!

我刚刚创建了从TextBlock:继承的自定义控件

using System.Windows.Controls;
namespace WpfApplication1
{
    public class CustomTextBlock : TextBlock
    {
    }
}

并在同一项目中使用:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1">
    <Grid>
        <local:CustomTextBlock Text="Hello" />
    </Grid>        
</Window>

因此答案是:

您可以从TextBlock继承

但是,为了在xaml中使用它,您必须首先编译项目。你的代码中可能有其他错误,阻止了项目的编译,因此你可能也会遇到类似的错误

The type 'local:CustomTextBlock' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built

或simmilar错误:

The name "CustomTextBlock" does not exist in the namespace "clr-namespace:WpfApplication1".

一旦你修复了其他错误,这些错误也会消失。

最新更新