Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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.
Posted
Comments
Sergey Alexandrovich Kryukov 17-Jan-16 23:34pm    
Why not simply make your control's background transparent? The user will see the container's background through your control's background, which will give you exactly the same effect, even better, because you won't need to solve the potential problem of shifted patterns.

It's just your claim that "using transparency will not work", but you have to explain it. Perhaps you mean something different, such as opacity of the control itself. This is not the same thing.

As to your idea, I don't understand why using some existing background can be a problem. All you need is to use Binding on the Background property.

—SA
cmbay 18-Jan-16 8:50am    
Thanks for your reply to my question. I have two issues with transparency. The first is that the control may be z-order above another control that I don't want to show thru. The second issue is that touch events are more difficult to capture when part of a control is transparent. So the question is, is it possible to bind the Background of my control to its' container's background without explicitly identifying the container? If it can be done how do I do it.
Sergey Alexandrovich Kryukov 18-Jan-16 11:37am    
I see, thank you for your explanation. As I say, you can bind one background to another. Please see Solution 1.
I up-voted the question; it's useful.
—SA

1 solution

cmbay asked:

…So the question is, is it possible to bind the Background of my control to its' container's background without explicitly identifying the container.
It can be done by binding using RelativeSource. Please see:
RelativeSource Class (System.Windows.Data)[^],
C# Corner: RelativeSources in WPF[^].

—SA
 
Share this answer
 
v3
Comments
cmbay 19-Jan-16 15:12pm    
Sergey,
I was able to bind to the Window's Background property using the XAML below as you suggested, thanks for the help. My implementation has the drawback of not binding to the nearest container in the visual tree. Also I don't see a way to override the binding by setting the Background property at a higher level. The implementation requires setting the ancestor's type so this prevents using the Background property of another context such as a Boarder without also changing the ancestor type. I am beginning to favor doing this in code by finding the nearest ancestor in my visual tree that has a Background property and setting the brush in that way.

Brush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Window}}, Path=Background }"
Sergey Alexandrovich Kryukov 19-Jan-16 15:19pm    
Hm... By nearest container, do you mean your some UI element is the immediate logical-tree parent of yours, which is not a window? Then why did you specify Window as ancestor type? As to your "nearest container", what is its type? Maybe it does not have the property "background"?
Could you show more of the context? Even better, could you simplify your XAML to show only essential detail (say only window, some container element and its content of just one UI element with binding) and show this XAML in full?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900