Skip to content

x:Bind not working with StateTrigger #9472

Open

Description

@vgromfeld

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

Activity

  1. added
    bug
    on Mar 21, 2024
  2. added
    needs-triage
    on Mar 21, 2024
  3. added
    team-Markup
    and removed
    needs-triage
    on Mar 22, 2024
  4. KWodarczyk commented on Oct 24, 2024

    @KWodarczyk

    This will not work in Window you need a Page

  5. michael-hawker commented on Nov 27, 2024

    @michael-hawker
    Contributor

    This will not work in Window you need a Page

    Vincent (@vgromfeld) did you try elsewhere?

    Otherwise I guess it's related to #5515

  6. WamWooWam commented on Jan 2, 2025

    @WamWooWam

    I've had this exact problem using VisualStateManager in ContentTemplates myself, trying to use x:Bind to enable StateTriggers. Notably, non-compiled bindings work just fine.

    This is not a Page vs. Window issue, and applies to WinUI and UWP.

  7. WamWooWam commented on Jan 2, 2025

    @WamWooWam

    Appears to be identical to #2572 but in C#

  8. added
    team-Core
    on Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-Bindingbugteam-Coreteam-Markup

Type

No type

Projects

  • Status
    Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions