microsoft-ui-xaml
microsoft-ui-xaml copied to clipboard
x:Bind throws NullReferenceException if it binds to StateTrigger.IsActive inside ControlTemplate
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
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
Any updates? This feature helps to use VisualState's triggers like in WPF
I'll attempt to repro this and get back.
Still happens. Also happens with DataTemplate + StateTrigger + IsActive = {x:Bind}
This issue also happen with the system XAML.
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.
for me adding x:name to the VisualState node fixed the null reference issue
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.
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.