50

What's the difference between the following two?

Background="{x:Null}"

and

Background="Transparent"
mobileTofu
  • 1,071
  • 2
  • 14
  • 25
  • 1
    Its exactly like setting a string `null` vs `string.Empty`. `null` will not initialize it. `String.Empty` will initialize it but put nothing. Same for Brushes. `x:null` means `null` and `Transparent` means initialized with no color and of course as other pointed out click functionality. – Nikhil Agrawal Oct 06 '16 at 04:37

5 Answers5

57

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.

zneak
  • 134,922
  • 42
  • 253
  • 328
HCL
  • 36,053
  • 27
  • 163
  • 213
  • 20
    Having a transparent background can also cause the control to be opaque to click/other events. i.e. you may be able to see another control through the transparent background, but click events won't make it through. This is in contrast to a null background, which would let the events through to the underlying control. – Adam Price Mar 17 '11 at 20:20
  • @Kent +1 didn't know that. What is with values in between 000000 and FFFFFF? – Markus Hütter Mar 17 '11 at 22:20
23

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.
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • Interesting note on `#00000000`. What about values between black and white. Will `#00AAAAAA` be hit test visible. And also what's the point in such difference, I do see it making any sense. – Snowbear Mar 17 '11 at 20:51
  • From what I can tell, `#00000000` is hit test visible while null isn't. If you place a `Border` over a `Button` and set the `Border` `Background` to `#00000000`, the `Button` isn't clickable. But with `{x:Null}` it is clickable. – Fredrik Hedblad Jun 27 '12 at 14:18
  • 1
    @Meleak: indeed, I just did some tests and found that to be the case, too. I thought maybe it was a behavioral change from earlier WPF releases, but I checked that too. Seems I was misguided - have updated my answer. – Kent Boogaart Jun 28 '12 at 08:19
11

{x:Null} will not be clickable, Transparent will.

Also see this.

Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
5

Elements with Transparent background receive mouse click events when clicking on background, elements with Null do not.

Snowbear
  • 16,924
  • 3
  • 43
  • 67
1

The Transparent brush

Will cause the background's alpha channel to be set to 0 which is 100% transparent

The {x:Null} value

Will cause the background to be set to the default control colour by WPF which is usually White on some properties like DataGrid.RowBackground and Transparent on most of the other properties.

It's a good habit to specify a brush colour since setting a brush to Null may result in undesired default colours.

Beyondo
  • 2,952
  • 1
  • 17
  • 42