Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

{x:Null} vs. Transparent?

Tags:

wpf

xaml

What's the difference between the following two?

Background="{x:Null}"

and

Background="Transparent"
like image 202
mobileTofu Avatar asked Mar 17 '11 20:03

mobileTofu


2 Answers

Transparent will create a brush that is initialized to a transparent color, null will set the property to null, this means that the destination property has not an brush attached. In WPF it's often important to set a brush to an element. If you for example want to track mouse downs in an element, you must set a background. If you don't want to set a solid color (make it opaque), you can use a transparent brush. This can be done with the string value "Transparent".
The difference lies in the manner, how the property will be set. If you assign null for a brush-property, the property will be set really to null. If you set the string "Transparent", the default value-converter that converts string to brushes converts this to the Brushes.Transparent brush.

Short version: {x:Null} sets the destination property to null. "Transparent" sets the destination property to a transparent brush.

like image 105
HCL Avatar answered Oct 22 '22 20:10

HCL


Both are setting the local value of the Background property. The former sets it to null and the latter sets it to Brushes.Transparent.

There are a couple of important points to be aware of:

  • Setting the value to null is not the same as not setting it at all. Since dependency properties obtain their effective value from multiple sources, setting a local value (even if it's null) can take precedence over values potentially sourced from elsewhere, such as a style or animation.
  • Another option for controlling hit test visibility is the IsHitTestVisible property. This property allows you to control hit test visibility regardless of the brush with which the UIElement is rendered.
like image 23
Kent Boogaart Avatar answered Oct 22 '22 19:10

Kent Boogaart