xamluwpuwp-xamlvisualstates

Can I change the Canvas.Zindex of an object using visual states?


How could I change the Canvas.Zindex of an object using visual states? I was expecting to be able to do something like this..

            <VisualState x:Name="MyVisualState">
                <VisualState.Setters>
                    <Setter Target="MyObject.Visibility" Value="Visible" />
                    <Setter Target="MyObject.Background" Value="Transparent" />
                    <Setter Target="MyObject.Canvas.ZIndex" Value="12" />
                </VisualState.Setters>
            </VisualState>

But this does not work. I have not been able to find any examples on how to do this. Can someone help?


Solution

  • Here you go. Note you need the () there because Canvas.ZIndex is an attached property and that's how you define the value of it in XAML.

    <VisualState x:Name="MyVisualState">
        <VisualState.Setters>
            <Setter Target="MyObject.Visibility"
                    Value="Visible" />
            <Setter Target="MyObject.Background"
                    Value="Transparent" />
    
            <Setter Target="MyObject.(Canvas.ZIndex)"
                    Value="12" />
        </VisualState.Setters>
    </VisualState>
    

    You might be interested in this answer which will show you how to generate the code above without writing a single line of code.