Skip to main content
 首页 » 编程设计

c#之在 WPF 中自定义 BulletChrome 元素

2024年02月24日33sky-heaven

我正在尝试在 WPF 中自定义复选框的外观。我希望我可以获取在 PresentationFramework.Aero 中定义的 BulletChrome 类的默认控件模板并使用它。但是,BulletChrome 不是 Control 的子类,因此没有可修改的模板。有谁知道我可以做到这一点?

请您参考如下方法:

如果你想放置某种 BulletChrome在 CheckBox 的模板中,其中 BulletDecorator在经典模板中,你可以这样做,因为 BulletChrome 继承自 Decorator , 它继承自 FrameworkElement .

下一部分,BulletChrome 是如何获得外观的?好吧,这是 WPF 中为数不多的完全不使用 xaml 处理渲染的部分之一:ButtonChrome 的视觉外观在其 OnRender() 中定义。方法。

BulletChrome.OnRender(),来自 Reflector:

protected override void OnRender(DrawingContext drawingContext) 
{ 
    Rect bounds = new Rect(0.0, 0.0, base.ActualWidth, base.ActualHeight); 
    this.DrawBackground(drawingContext, ref bounds); 
    this.DrawDropShadows(drawingContext, ref bounds); 
    this.DrawBorder(drawingContext, ref bounds); 
    this.DrawInnerBorder(drawingContext, ref bounds); 
} 

不幸的是,由于在 OnRender() 中调用的所有这些方法都是私有(private)的,因此即使您对 ButtonChrome 进行子类化,也不能乱用它们,也许只能在顶部覆盖一些渲染。

所以基本上,您要么开始挖掘 Reflector 中的渲染代码并尝试根据您的需要调整和重写它,要么从头开始构建您自己的模板/装饰器/任何东西。 (但是只要它有效,你使用什么真的不重要,对吧?)

希望这能回答您的问题,干杯。