WPF自定义控件可以使用控件容器的背景画笔。 [英] Can a WPF Custom control use the Background Brush of the control's container.

查看:285
本文介绍了WPF自定义控件可以使用控件容器的背景画笔。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个WPF自定义控件模板,我希望其中一个GeometryDrawing Brushs使用控件容器的Background Brush。例如,如果我将此控件放在一个Window上,我希望这个特殊的Brush使用Window的Background Brush。如果控件放在网格上就好了。我确信这在代码背后是可能的,但我希望找到一个XAML解决方案,尽可能少地假设上下文。使用透明度不适用于此应用程序。

I am creating a WPF Custom Control Template and I would like one of the GeometryDrawing Brushs to use the Background Brush of the control's container. For example if I put this control on a Window I would like this particular Brush to use the Window's Background Brush. Like wise if the control were place on a Grid. I am sure this is possible in code behind but I was hoping to find a XAML solution that made as few assumptions as possible about the context . Using transparency will not work for this application.

推荐答案

cmbay问:



...问题是,是否可以将我的控件的背景绑定到其容器的背景而无需明确标识容器。


…So the question is, is it possible to bind the Background of my control to its' container's background without explicitly identifying the container.

可以通过使用的RelativeSource 。请参阅:

RelativeSource类(System.Windows.Data) [ ^ ],

C#Corner:WPF中的RelativeSources [ ^ ]。



-SA

It can be done by binding using RelativeSource. Please see:
RelativeSource Class (System.Windows.Data)[^],
C# Corner: RelativeSources in WPF[^].

—SA


这篇关于WPF自定义控件可以使用控件容器的背景画笔。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆