microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

x:Bind throws NullReferenceException if it binds to StateTrigger.IsActive inside ControlTemplate

Open alexdi220 opened this issue 4 years ago • 7 comments

Describe the bug Hi, I faced the nullref when I try to use StateTrigger and x:Bind. The template of CustomControl has a visual state with a trigger. IsActive property binds to IsEnalbed property of the control.

Steps to reproduce the bug MainWindow.xaml

<Window
    x:Class="IssueSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:IssueSample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel.Resources>
            <SolidColorBrush x:Key="MyStateBackground" Color="Black"/>
            <Style x:Key="style" TargetType="local:CustomControl">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="local:CustomControl">
                            <Grid>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal" />
                                        <VisualState>
                                            <VisualState.StateTriggers>
                                                <StateTrigger IsActive="{x:Bind IsEnabled, Mode=OneWay}"/>
                                            </VisualState.StateTriggers>
                                            <VisualState.Setters>
                                                <Setter Target="root.Background" Value="{StaticResource DisabledBackground}" />
                                            </VisualState.Setters>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Border Width="100" Height="100" x:Name="root" Background="Red"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </StackPanel.Resources>
        <local:CustomControl Style="{StaticResource style}"/>
    </StackPanel>
</Window>

MainWindow.xaml.cs

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace IssueSample {
    public sealed partial class MainWindow : Window {
        public MainWindow() {
            this.InitializeComponent();
        }
    }
    public class CustomControl : Control { }
}

Expected behavior The StateTrigger should work

Screenshots image

Version Info

NuGet package version: [Microsoft.WinUI 3.0.0-preview2.200713.0]

Windows app type:

UWP Win32
Yes
Windows 10 version Saw the problem?
Insider Build (xxxxx)
May 2020 Update (19041) Yes
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context

IssueSample.zip

alexdi220 avatar Oct 27 '20 11:10 alexdi220

This issue is reproduced in Preview 3.

Updated project:

IssueSample.zip

BorzillaR avatar Nov 19 '20 13:11 BorzillaR

Any updates? This feature helps to use VisualState's triggers like in WPF

alexdi220 avatar Feb 02 '21 10:02 alexdi220

I'll attempt to repro this and get back.

JeanRoca avatar Feb 03 '21 23:02 JeanRoca

Still happens. Also happens with DataTemplate + StateTrigger + IsActive = {x:Bind}

CSPshala avatar May 04 '21 19:05 CSPshala

This issue also happen with the system XAML.

vgromfeld avatar Oct 01 '21 09:10 vgromfeld

I'm seeing something similar. Trying to follow the pattern in the sample at https://github.com/microsoft/Windows-universal-samples/blob/main/Samples/XamlStateTriggers/cs/CustomTriggers/ControlSizeTrigger.cs, doesn't seem to work with any sort of binding when the visual states/triggers are defined within a ControlTemplate.

nCastle1 avatar Apr 06 '22 19:04 nCastle1

for me adding x:name to the VisualState node fixed the null reference issue

KWodarczyk avatar Sep 14 '22 16:09 KWodarczyk

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Jul 28 '23 21:07 github-actions[bot]

I have faced this issue today. Adding x:Name has no effect. I'm using Windows App SDK 1.5.240311000.

I can't believe such issue was simply ignored.

Khiro95 avatar Apr 30 '24 21:04 Khiro95