microsoft / microsoft-ui-xaml

WinUI: a modern UI framework with a rich set of controls and styles to build dynamic and high-performing Windows applications.
MIT License
6.39k stars 683 forks source link

x:Bind not working with StateTrigger #9472

Open vgromfeld opened 8 months ago

vgromfeld commented 8 months ago

Describe the bug

The application crash when using x:Bind to set the StateTrigger.IsActive property.

Steps to reproduce the bug

Add the following XAML code in the MainWindow XAML of a blank WinAppSDK C# app:

<StackPanel>
    <ToggleButton
        x:Name="toggleStates"
        Content="Toggle states"
        IsChecked="False" />

    <TextBlock x:Name="toggleStatus" Text="Off" />

    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <VisualState>
                <VisualState.StateTriggers>
                    <StateTrigger IsActive="{x:Bind toggleStates.IsChecked.Value, Mode=OneWay}" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="toggleStatus.Text" Value="On" />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
</StackPanel>

Run the application. It crashes at startup because the StateTrigger instance is null in

private static class XamlBindingSetters
{
   public static void Set_Microsoft_UI_Xaml_StateTrigger_IsActive(global::Microsoft.UI.Xaml.StateTrigger obj, global::System.Boolean value)
    {
        obj.IsActive = value;  // <-- obj is null
    }
};

image

Expected behavior

The generated code should work as in UWP apps where this was working without any issue.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.5.1: 1.5.240311000

Windows version

Windows 11 (22H2): Build 22621

Additional context

No response

KWodarczyk commented 1 month ago

This will not work in Window you need a Page

michael-hawker commented 1 week ago

This will not work in Window you need a Page

@vgromfeld did you try elsewhere?

Otherwise I guess it's related to https://github.com/microsoft/microsoft-ui-xaml/issues/5515