Enables a state if the value is equal to, greater than, or less than another value.
XAML
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. --><Pagex:Class="TriggersExperiment.Samples.CompareStateTriggerSample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="using:TriggersExperiment.Samples"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:triggers="using:CommunityToolkit.WinUI"mc:Ignorable="d"><Grid><VisualStateManager.VisualStateGroups><VisualStateGroup><VisualStatex:Name="Less"><VisualState.StateTriggers><triggers:CompareStateTriggerComparison="LessThan"Value="{Binding Value, ElementName=Slider, Mode=OneWay}"To="3" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="SliderStatus.Text"Value="Slider value is less than 3" /></VisualState.Setters></VisualState><VisualStatex:Name="Equal"><VisualState.StateTriggers><triggers:CompareStateTriggerComparison="Equal"Value="{Binding Value, ElementName=Slider, Mode=OneWay}"To="3" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="SliderStatus.Text"Value="Slider value is 3" /></VisualState.Setters></VisualState><VisualStatex:Name="Greater"><VisualState.StateTriggers><triggers:CompareStateTriggerComparison="GreaterThan"Value="{Binding Value, ElementName=Slider, Mode=OneWay}"To="3" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="SliderStatus.Text"Value="Slider value is greater than 3" /></VisualState.Setters></VisualState></VisualStateGroup><VisualStateGroup><VisualStatex:Name="GreaterThanOrEqual"><VisualState.StateTriggers><triggers:CompareStateTriggerComparison="GreaterThanOrEqual"Value="{Binding Value, ElementName=Slider2, Mode=OneWay}"To="4" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="SliderStatus2.Text"Value="Slider value is 4 or greater" /></VisualState.Setters></VisualState><VisualStatex:Name="LessThanOrEqual"><VisualState.StateTriggers><triggers:CompareStateTriggerComparison="LessThanOrEqual"Value="{Binding Value, ElementName=Slider2, Mode=OneWay}"To="2" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="SliderStatus2.Text"Value="Slider value is 2 or less" /></VisualState.Setters></VisualState></VisualStateGroup></VisualStateManager.VisualStateGroups><StackPanelMaxWidth="400"><Sliderx:Name="Slider"Maximum="5"Minimum="0" /><TextBlockx:Name="SliderStatus"Style="{StaticResource CaptionTextBlockStyle}"Text="If you see this, trigger isn't working" /><Sliderx:Name="Slider2"Margin="0,36,0,0"Maximum="5"Minimum="0" /><TextBlockx:Name="SliderStatus2"Style="{StaticResource CaptionTextBlockStyle}"Text="Slider value is 3" /></StackPanel></Grid></Page>
C#
// Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the MIT license.// See the LICENSE file in the project root for more information.using CommunityToolkit.WinUI;
namespaceTriggersExperiment.Samples;
[ToolkitSample(id: nameof(CompareStateTriggerSample), "CompareStateTrigger", description: $"A sample for showing how to create and use a {nameof(CompareStateTrigger)}.")]
publicsealedpartialclassCompareStateTriggerSample : Page
{
publicCompareStateTriggerSample()
{
this.InitializeComponent();
}
}
ControlStateTrigger
Enables a state if the target control meets the specified size
XAML
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. --><Pagex:Class="TriggersExperiment.Samples.ControlSizeTriggerSample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="using:TriggersExperiment.Samples"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:triggers="using:CommunityToolkit.WinUI"mc:Ignorable="d"><Grid><VisualStateManager.VisualStateGroups><VisualStateGroup><VisualState><VisualState.StateTriggers><triggers:ControlSizeTriggerMinWidth="400"MaxWidth="501"TargetElement="{Binding ElementName=ParentGrid}" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="ResizingText.Text"Value="more than 400" /></VisualState.Setters></VisualState></VisualStateGroup></VisualStateManager.VisualStateGroups><StackPanelMaxWidth="400"><Sliderx:Name="Slider"Maximum="500"Minimum="1" /><Gridx:Name="ParentGrid"Width="{Binding Value, ElementName=Slider, Mode=OneWay}"Height="32"Background="{ThemeResource AccentFillColorDefaultBrush}"CornerRadius="{StaticResource ControlCornerRadius}" /><TextBlockMargin="0,12,0,0"HorizontalAlignment="Center"Style="{StaticResource CaptionTextBlockStyle}"><RunText="Control size is" /><Runx:Name="ResizingText"FontWeight="SemiBold"Text="less than 400" /></TextBlock></StackPanel></Grid></Page>
C#
// Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the MIT license.// See the LICENSE file in the project root for more information.using CommunityToolkit.WinUI;
namespaceTriggersExperiment.Samples;
[ToolkitSample(id: nameof(ControlSizeTriggerSample), "ControlStateTrigger", description: $"A sample for showing how to create and use a {nameof(ControlSizeTrigger)}.")]
publicsealedpartialclassControlSizeTriggerSample : Page
{
publicControlSizeTriggerSample()
{
this.InitializeComponent();
}
}
IsEqualStateTrigger
Enables a state if the value is equal to another value.
XAML
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. --><Pagex:Class="TriggersExperiment.Samples.IsEqualStateTriggerSample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="using:TriggersExperiment.Samples"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:triggers="using:CommunityToolkit.WinUI"mc:Ignorable="d"><Grid><VisualStateManager.VisualStateGroups><VisualStateGroup><VisualStatex:Name="checkstate"><VisualState.StateTriggers><!-- Checkbox is null when indeterminate --><triggers:IsEqualStateTriggerValue="{Binding IsChecked, ElementName=checkbox, Mode=OneWay}"To="{x:Null}" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="checkStatus.Text"Value="indeterminate" /></VisualState.Setters></VisualState></VisualStateGroup><VisualStateGroup><VisualStatex:Name="sliderstate"><VisualState.StateTriggers><triggers:IsEqualStateTriggerValue="{Binding Value, ElementName=slider, Mode=OneWay}"To="0" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="sliderStatus.Text"Value="0" /></VisualState.Setters></VisualState></VisualStateGroup></VisualStateManager.VisualStateGroups><StackPanelMaxWidth="400"><CheckBoxx:Name="checkbox"Content="Check me"IsChecked="True"IsThreeState="True" /><TextBlockMargin="0,8,0,0"Style="{StaticResource CaptionTextBlockStyle}"><RunText="Checkbox value is" /><Runx:Name="checkStatus"FontWeight="SemiBold"Text="not indeterminate" /></TextBlock><Sliderx:Name="slider"Margin="0,36,0,0"Maximum="5"Minimum="0" /><TextBlockMargin="0,8,0,0"Style="{StaticResource CaptionTextBlockStyle}"><RunText="Slider value is" /><Runx:Name="sliderStatus"FontWeight="SemiBold"Text="more than 0" /></TextBlock></StackPanel></Grid></Page>
C#
// Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the MIT license.// See the LICENSE file in the project root for more information.using CommunityToolkit.WinUI;
namespaceTriggersExperiment.Samples;
[ToolkitSample(id: nameof(IsEqualStateTriggerSample), "IsEqualStateTrigger", description: $"A sample for showing how to create and use a {nameof(IsEqualStateTrigger)}.")]
publicsealedpartialclassIsEqualStateTriggerSample : Page
{
publicIsEqualStateTriggerSample()
{
this.InitializeComponent();
}
}
IsNotEqualStateTrigger
Enables a state if the value is not equal to another value.
XAML
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. --><Pagex:Class="TriggersExperiment.Samples.IsNotEqualStateTriggerSample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="using:TriggersExperiment.Samples"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:triggers="using:CommunityToolkit.WinUI"mc:Ignorable="d"><Grid><VisualStateManager.VisualStateGroups><VisualStateGroup><VisualStatex:Name="checkstate"><VisualState.StateTriggers><!-- Checkbox is null when indeterminate --><triggers:IsNotEqualStateTriggerValue="{Binding IsChecked, ElementName=checkbox, Mode=OneWay}"To="{x:Null}" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="checkStatus.Text"Value="not indeterminate" /></VisualState.Setters></VisualState></VisualStateGroup><VisualStateGroup><VisualStatex:Name="sliderstate"><VisualState.StateTriggers><triggers:IsNotEqualStateTriggerValue="{Binding Value, ElementName=slider, Mode=OneWay}"To="0" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="sliderStatus.Text"Value="not 0" /></VisualState.Setters></VisualState></VisualStateGroup></VisualStateManager.VisualStateGroups><StackPanelMaxWidth="400"><CheckBoxx:Name="checkbox"Content="Check me"IsChecked="True"IsThreeState="True" /><TextBlockMargin="0,8,0,0"Style="{StaticResource CaptionTextBlockStyle}"><RunText="Checkbox value is" /><Runx:Name="checkStatus"FontWeight="SemiBold"Text="indeterminate" /></TextBlock><Sliderx:Name="slider"Margin="0,36,0,0"Maximum="5"Minimum="0" /><TextBlockMargin="0,8,0,0"Style="{StaticResource CaptionTextBlockStyle}"><RunText="Slider value is" /><Runx:Name="sliderStatus"FontWeight="SemiBold"Text="0" /></TextBlock></StackPanel></Grid></Page>
C#
// Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the MIT license.// See the LICENSE file in the project root for more information.using CommunityToolkit.WinUI;
namespaceTriggersExperiment.Samples;
[ToolkitSample(id: nameof(IsNotEqualStateTriggerSample), "IsNotEqualStateTrigger", description: $"A sample for showing how to create and use a {nameof(IsNotEqualStateTrigger)}.")]
publicsealedpartialclassIsNotEqualStateTriggerSample : Page
{
publicIsNotEqualStateTriggerSample()
{
this.InitializeComponent();
}
}
IsNullOrEmptyStateTrigger
Enables a state if an Object is null or a String/IEnumerable is empty.
XAML
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. --><Pagex:Class="TriggersExperiment.Samples.IsNullOrEmptyStateTriggerSample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="using:TriggersExperiment.Samples"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:triggers="using:CommunityToolkit.WinUI"mc:Ignorable="d"><Grid><VisualStateManager.VisualStateGroups><VisualStateGroupx:Name="TextBoxStates"><VisualStatex:Name="TextBoxNotEmptyState" /><VisualStatex:Name="TextBoxEmptyState"><VisualState.StateTriggers><triggers:IsNullOrEmptyStateTriggerValue="{Binding Text, ElementName=OurTextBox, Mode=OneWay}" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="OurTextBox.BorderBrush"Value="Red" /><SetterTarget="OurTextBoxError.Visibility"Value="Visible" /></VisualState.Setters></VisualState></VisualStateGroup><VisualStateGroupx:Name="ListBoxStates"><VisualStatex:Name="ListNotEmptyState" /><VisualStatex:Name="ListEmptyState"><VisualState.StateTriggers><triggers:IsNullOrEmptyStateTriggerValue="{Binding Items, ElementName=OurList, Mode=OneWay}" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="RemoveButton.IsEnabled"Value="False" /><SetterTarget="ListEmptyMessage.Visibility"Value="Visible" /></VisualState.Setters></VisualState></VisualStateGroup></VisualStateManager.VisualStateGroups><StackPanelHorizontalAlignment="Center"Spacing="12"><TextBoxx:Name="OurTextBox"Header="The TextBox below will warn if it is empty."Text="" /><TextBlockx:Name="OurTextBoxError"VerticalAlignment="Center"Foreground="{ThemeResource SystemFillColorCriticalBrush}"Text="* required"Visibility="Collapsed" /><TextBlockx:Name="ListEmptyMessage"Margin="0,24,0,0"Text="List is empty, add some items"Visibility="Collapsed" /><StackPanelOrientation="Horizontal"><Buttonx:Name="AddButton"Margin="0,0,4,0"Click="AddButton_Click"Content="Add" /><Buttonx:Name="RemoveButton"Click="RemoveButton_Click"Content="Remove" /></StackPanel><ListViewx:Name="OurList"HorizontalAlignment="Left"><ListView.ItemTemplate><DataTemplatex:DataType="x:String"><Grid><TextBlockText="{Binding}" /></Grid></DataTemplate></ListView.ItemTemplate></ListView></StackPanel></Grid></Page>
C#
// Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the MIT license.// See the LICENSE file in the project root for more information.using CommunityToolkit.WinUI;
namespaceTriggersExperiment.Samples;
[ToolkitSample(id: nameof(IsNullOrEmptyStateTriggerSample), "IsNullOrEmptyStateTrigger", description: $"A sample for showing how to create and use a {nameof(IsNullOrEmptyStateTrigger)}.")]
publicsealedpartialclassIsNullOrEmptyStateTriggerSample : Page
{
publicIsNullOrEmptyStateTriggerSample()
{
this.InitializeComponent();
}
privatevoidAddButton_Click(object sender, RoutedEventArgs e)
{
if (OurList != null)
{
OurList.Items.Add("Item");
}
}
privatevoidRemoveButton_Click(object sender, RoutedEventArgs e)
{
if (OurList != null)
{
OurList.Items.RemoveAt(0);
}
}
}
NetworkConnectionStateTrigger
Trigger for switching when the network availability changes.
XAML
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. --><Pagex:Class="TriggersExperiment.Samples.NetworkConnectionStateTriggerSample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="using:TriggersExperiment.Samples"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:triggers="using:CommunityToolkit.WinUI"mc:Ignorable="d"><Grid><VisualStateManager.VisualStateGroups><VisualStateGroup><VisualStatex:Name="connected"><VisualState.StateTriggers><triggers:NetworkConnectionStateTriggerConnectionState="Connected" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="connectionStatus.Text"Value="Internet is available" /></VisualState.Setters></VisualState><VisualStatex:Name="disconnected"><VisualState.StateTriggers><triggers:NetworkConnectionStateTriggerConnectionState="Disconnected" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="connectionStatus.Text"Value="No internet connection" /></VisualState.Setters></VisualState></VisualStateGroup></VisualStateManager.VisualStateGroups><TextBlockHorizontalAlignment="Center"><RunText="Connection status:" /><Runx:Name="connectionStatus"FontWeight="SemiBold" /></TextBlock></Grid></Page>
C#
// Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the MIT license.// See the LICENSE file in the project root for more information.using CommunityToolkit.WinUI;
namespaceTriggersExperiment.Samples;
[ToolkitSample(id: nameof(NetworkConnectionStateTriggerSample), "NetworkConnectionStateTrigger", description: $"A sample for showing how to create and use a {nameof(NetworkConnectionStateTrigger)}.")]
publicsealedpartialclassNetworkConnectionStateTriggerSample : Page
{
publicNetworkConnectionStateTriggerSample()
{
this.InitializeComponent();
}
}
RegexStateTrigger
Enables a state if the regex expression is true for a given string value.
XAML
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. --><Pagex:Class="TriggersExperiment.Samples.RegexStateTriggerSample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="using:TriggersExperiment.Samples"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:triggers="using:CommunityToolkit.WinUI"mc:Ignorable="d"><Grid><VisualStateManager.VisualStateGroups><VisualStateGroup><VisualStatex:Name="validEmail"><VisualState.StateTriggers><!-- Note: Simple example RegEx, see our IsEmail string extension using emailregex.com for official RFC 5322 support --><triggers:RegexStateTriggerExpression="^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"Options="IgnoreCase"Value="{Binding Text, ElementName=emailTextBox, Mode=OneWay}" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="emailStatus.Text"Value="This is a valid email" /><SetterTarget="emailStatus.Foreground"Value="{ThemeResource SystemFillColorSuccessBrush}" /><SetterTarget="submitButton.IsEnabled"Value="true" /></VisualState.Setters></VisualState></VisualStateGroup></VisualStateManager.VisualStateGroups><StackPanelMaxWidth="400"><TextBoxx:Name="emailTextBox"HorizontalAlignment="Stretch"Header="Enter an email" /><TextBlockx:Name="emailStatus"Margin="0,4,0,24"Foreground="{ThemeResource SystemFillColorCriticalBrush}"Style="{StaticResource CaptionTextBlockStyle}"Text="Not a valid email" /><Buttonx:Name="submitButton"Content="Submit"IsEnabled="False" /></StackPanel></Grid></Page>
C#
// Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the MIT license.// See the LICENSE file in the project root for more information.using CommunityToolkit.WinUI;
namespaceTriggersExperiment.Samples;
[ToolkitSample(id: nameof(RegexStateTriggerSample), "RegexStateTrigger", description: $"A sample for showing how to create and use a {nameof(RegexStateTrigger)}.")]
publicsealedpartialclassRegexStateTriggerSample : Page
{
publicRegexStateTriggerSample()
{
this.InitializeComponent();
}
}
UserHandPreferenceStateTrigger
Trigger for switching UI based on whether the user favors their left or right hand.
XAML
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. --><Pagex:Class="TriggersExperiment.Samples.UserHandPreferenceStateTriggerSample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="using:TriggersExperiment.Samples"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:triggers="using:CommunityToolkit.WinUI"mc:Ignorable="d"><Grid><VisualStateManager.VisualStateGroups><VisualStateGroup><VisualStatex:Name="leftHanded"><VisualState.StateTriggers><triggers:UserHandPreferenceStateTriggerHandPreference="LeftHanded" /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="handPreferenceStatus.Text"Value="You are left-handed" /></VisualState.Setters></VisualState><VisualStatex:Name="rightHanded"><VisualState.StateTriggers><triggers:UserHandPreferenceStateTrigger /></VisualState.StateTriggers><VisualState.Setters><SetterTarget="handPreferenceStatus.Text"Value="You are right-handed" /></VisualState.Setters></VisualState></VisualStateGroup></VisualStateManager.VisualStateGroups><StackPanelHorizontalAlignment="Center"><TextBlockx:Name="handPreferenceStatus"HorizontalAlignment="Center"FontWeight="SemiBold" /><TextBlockMargin="0,12"HorizontalAlignment="Center"Style="{StaticResource CaptionTextBlockStyle}"Text="Set your hand preference in Windows settings (Devices -> Pen) then restart the app."TextWrapping="WrapWholeWords" /></StackPanel></Grid></Page>
C#
// Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the MIT license.// See the LICENSE file in the project root for more information.using CommunityToolkit.WinUI;
namespaceTriggersExperiment.Samples;
[ToolkitSample(id: nameof(UserHandPreferenceStateTriggerSample), "UserHandPreferenceStateTrigger", description: $"A sample for showing how to create and use a {nameof(UserHandPreferenceStateTrigger)}.")]
publicsealedpartialclassUserHandPreferenceStateTriggerSample : Page
{
publicUserHandPreferenceStateTriggerSample()
{
this.InitializeComponent();
}
}
Surađujte s nama na GitHubu
Izvor za ovaj sadržaj možete pronaći na GitHubu, gdje možete stvarati i pregledavati probleme i zahtjeve za povlačenjem. Dodatne informacije potražite u našem vodiču za suradnike.
Povratne informacije o proizvodu Windows Community Toolkit
Windows Community Toolkit je projekt otvorenog koda. Odaberite vezu za slanje povratnih informacija:
Pridružite se seriji susreta kako biste s kolegama programerima i stručnjacima izgradili skalabilna rješenja umjetne inteligencije temeljena na stvarnim slučajevima upotrebe.