【问题标题】:WPF Custom Controls Are InvisibleWPF 自定义控件不可见
【发布时间】:2011-07-14 15:35:50
【问题描述】:

当我在 WPF 中创建自定义控件并将其添加到窗口时,我在对话框中的放置位置看不到任何内容。这就是我正在做的事情:

  1. 创建一个新的 WPF 应用程序
  2. 添加 -> 新项目... -> 自定义控件 (WPF):“CustomButton.cs”
  3. 我将 CustomButton 基类更改为 Button 而不是 Control
  4. 在我的主窗口中添加一个 CustomButton 控件。
  5. 当我运行应用程序或在设计器中查看主窗口时,我什么也看不到。

代码如下所示。

CustomButton.cs:

public class CustomButton : Button
{
    static CustomButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton),
            new FrameworkPropertyMetadata(typeof(CustomButton)));
    }
}

MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication1">
    <Grid>
        <my:CustomButton Content="Hello World" x:Name="customButton1"
                         HorizontalAlignment="Left" VerticalAlignment="Top" Margin="150,175,0,0" />
    </Grid>
</Window>

Generic.xaml:

<Style TargetType="{x:Type local:CustomButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomButton}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我找到了两条关于发生了什么的线索,但还没有任何结果。当我添加自定义控件时,Visual Studio 添加了 Themes/Generic.xaml,但无论我在那里尝试什么,我都看不到屏幕上有什么不同。另一件事是,如果我注释掉 CustomButton.cs 中的静态构造函数,按钮会突然出现在主窗口中。不过,它在所有情况下看起来并不完全正确(例如,如果我使用工具栏中的按钮)。

【问题讨论】:

    标签: wpf custom-controls


    【解决方案1】:

    我想你已经找到了解决问题的方法。 但是,对于这种情况,其他任何人都会遇到与您相同的问题: 尽管创建自定义控件的所有步骤都已正确完成,但没有显示自定义控件的唯一解释可能是 AssemblyInfo.cs 中缺少条目。 此文件必须包含以下条目:

    [assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly))]
    

    没有此条目,generic.xaml 文件将被忽略,因此找不到 default-control-template,因此控件根本不会获得 control-template,因此不会显示。这也解释了为什么当你禁用它的静态构造函数时你的控件突然出现了。行:

    DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton), new FrameworkPropertyMetadata(typeof(CustomButton)));
    

    告诉控件使用它自己的默认样式而不是从它的基类继承它。因此,如果没有这一行,CustomButton 将简单地重用 Button 类的默认控件模板,结果是,您在 generic.xaml 中写入的任何内容都不会对 CustomButton 产生任何影响。

    【讨论】:

    • 我无法确认这一点,我在 AssemblyInfo 文件中有条目,但它仍然不起作用。
    • 该死的,这是我的问题,忘记在资源中包含样式文件。谢谢!
    • 这个答案节省了我的$$!谢谢
    【解决方案2】:

    您的自定义控件模板在哪里?

    说出来

          DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton),
            new FrameworkPropertyMetadata(typeof(CustomButton)));
    

    您表示要定义自己的自定义控件。我想如果你删除它,你会看到你的按钮。

    【讨论】:

    • 你说得对,当我删除它时它会显示出来。就像我说的,不过,它添加了 Generic.xaml,看起来它里面有自定义控件模板。我也将其添加到主要问题中。
    • 它看起来不像模板 :) 它只会绘制一个(可能是空的)边框。
    • 另外,听起来您并不想在这里制作自定义控件……是吗?似乎您只想要一个自定义的 Button 子类。
    • 好的,我想我明白了。需要更多地研究样式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 2019-03-14
    • 2011-05-28
    • 2014-08-08
    相关资源
    最近更新 更多