Search code examples
xamlwindows-store-appsuwp

Why is {x:Null} no longer a valid value in a Style Setter in UWP?


A follow up to this question, why is {x:Null} no longer a valid option for Setter.Value?

Put this code in a resource dictionary in your UWP app:

<Style x:Key="MyList"
        TargetType="ListView">
    <Setter Property="Transitions" 
            Value="{x:Null}"/>
</Style>

And use it in any ListView:

<ListView Style="{StaticResource MyList}"/>

Your app will crash. A nasty crash as well, the kind that takes down the app without breaking properly in your debugger.

Also, the designer complains with a Catastrophic failure:

enter image description here

I know that setting values to {x:Null} is valid. If you set the <ListView Transitions="{x:Null}"/> directly, then it works fine... as expected.

So... what happened with style setters? ... Care to explain, Microsoft? Is there at least an alternate method?


Solution

  • It is really a weird behavior and overall because as you explained setting directly to null works and with the style does not. The only alternative I found is just set a clear transitioncollection:

    <Page.Resources>
    <Style x:Key="MyList" TargetType="ListView">
        <Setter Property="Transitions" >
            <Setter.Value>
                <TransitionCollection></TransitionCollection>
            </Setter.Value>
        </Setter>
        <Setter Property="DataContext" Value="{x:Null}"/>
    </Style>
    

    I set the example of the DataContext that can be set to null, it says the same error but compiles and works.

    I know it is not the best solution but it is a workaround to have a clear transitioncollection.