Share via

How to apply style to Window in Winui3?

Sachi 221 Reputation points
2022-03-15T05:05:07.707+00:00

Hi I want use input triggers in winui3 im following below document how to apply style to window ?
https://learn.microsoft.com/en-us/windows/apps/design/layout/layouts-with-xaml#visual-states-and-styles
like
<Window.Resources> //which not support in winui3

Plz help me

Windows development | Windows App SDK

Answer accepted by question author

Castorix31 91,871 Reputation points
2022-03-16T12:39:42.087+00:00

It is Application.Current.Resources in winUI (or StaticResource in XAML) and VisualStateManager must be inside a Page or it does not work :

Test by changing the Style of a Button to AccentButtonStyle on mouse click (with MS InputTypeTrigger.cs for the click) =>

183710-visualstatemanager.gif

Test code (remove _ at Target_PropertyPath (editor bug) :

        var vsg = new VisualStateGroup();  
        var vs = new VisualState();  
        vs.StateTriggers.Add(new CustomTriggers.InputTypeTrigger { TargetElement = myButton, PointerType = Microsoft.UI.Input.PointerDeviceType.Mouse });  
        vs.Setters.Add(new Setter { Target = new Target_PropertyPath { Path = new PropertyPath("(Button.Style)"), Target = button2 }, Value = Application.Current.Resources["AccentButtonStyle"] });  
    vsg.States.Add(vs);  
        VisualStateManager.GetVisualStateGroups(grid1).Add(vsg);  

XAML (change "CSharp_WinUI" ) :

<Window  
    x:Class="CSharp_WinUI.MainWindow"  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:local="using:CSharp_WinUI"  
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
    xmlns:muxc="using:Microsoft.UI.Xaml.Controls"  
    xmlns:triggers="using:CSharp_WinUI.CustomTriggers"  
    mc:Ignorable="d">  
    <Page x:Name="page1">  
        <Grid x:Name="grid1" >   
            <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal"  Spacing="12">  
                <Button x:Name="myButton"  
                    Click="myButton_Click"  
                    Content="Click Me" >  
                </Button>  
                <CheckBox x:Name="checkBoxState1"  Content="visualState1" />                 
                <Button  x:Name="button2"  Content="Button" />  
            </StackPanel>  
        </Grid>  
    </Page>  
</Window>  

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.