【发布时间】:2019-10-14 04:53:11
【问题描述】:
在我的 WPF 应用程序中,我在多个页面上有多个标签,它们看起来都一样,但在程序运行期间可能会改变样式(一次全部)。
经过大量在线搜索后,我浏览了 Window.Resources 模板和用户控件(在程序运行期间可能进行更改时,它们都不能很好地用于样式设置)并且目前已选择 CustomControl。但是,我不知道如何设置它的内容。有些标签会有一个带有多个孙子的包装面板,有些标签只有文字。但是,无论哪种方式,我都无法解决。这似乎是一个超越标签的程序。我错过了什么?
我的控件(刚改成继承自Label):
public class MyControl : Label
{
static MyControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
}
}
Themes.Generic.xaml 没有变化
My Window XAML(此处为测试目的设置样式):
<local:MyControl Height="100">
<TextBlock Text="hi there" FontSize="60"></TextBlock>
<!-- More children will be needed, but lets start with one for now -->
</local:MyControl>
<local:MyControl Height="100" Content="this should exist" FontSize="50" />
如何让该内容显示出来?
编辑:删除 DefaultStyleKeyProperty.OverrideMetadata 有效。
【问题讨论】: