microsoft-ui-xaml
microsoft-ui-xaml copied to clipboard
WinUI 3 StateTrigger IsActive not working
Describe the bug
x:Bind with StateTrigger IsActive does not wrok at all
Steps to reproduce the bug
Just copy paste the code below, the foregorund should change to green on page load and it should change color on button click but its always blue.
<Window
x:Class="WinUI3DesktopVisualStateManager.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinUI3DesktopVisualStateManager"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel x:Name="rootPanel" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="SomeState">
<VisualState.StateTriggers>
<StateTrigger IsActive="{x:Bind TogglerMe, Mode=OneWay}" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="TextBlockTest.Foreground" Value="Green" />
<Setter Target="TextBlockTest.Text" Value="Triggered" />
<Setter Target="TEstpanel.Background" Value="Green" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
<StackPanel Height="10" Width="10" x:Name="TEstpanel"></StackPanel>
<TextBlock Text="Some Text" Margin="20 0 0 0" Foreground="Blue" x:Name="TextBlockTest"/>
</StackPanel>
</Window>
code behind
public sealed partial class MainWindow : Window, INotifyPropertyChanged
{
private bool _toggleMe = true;
public bool TogglerMe { get { return _toggleMe; } set { _toggleMe = value; OnPropertyChanged(nameof(TogglerMe)); } }
public MainWindow()
{
this.InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
private void myButton_Click(object sender, RoutedEventArgs e)
{
TogglerMe = !TogglerMe;
}
public void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
Expected behavior
StateTrigger IsActive should change the foreground color
Screenshots
No response
NuGet package version
WinUI 3 - Windows App SDK 1.1.4
Windows app type
- [ ] UWP
- [ ] Win32
Device form factor
Desktop
Windows version
No response
Additional context
No response
This seems similar to this thread in MS forums, where I had to use a Page for VisualStateManager
yeah i can confirm that in Page it works fine but that is not the solution, we need to know why it fails in window and make it work in window. None of the docs mention that it does not work in window so it's not nice if it's know issue it needs to be communicated otherwise people will get stuck with the same issue wasting time on fixing it like i did.
VisualStateManager only applies to classes that derive from Control (documented here]. Window is not a Control (it's not even a DependencyObject although #7305 is a proposal to address that) and so VisualStateManager does not work with it.