Skip to content

Commit 6a05a3c

Browse files
authored
Add FocusBehavior and AutoFocusBehavior (#3462)
* add focus behaviors * cleaned csproj * changed xaml code to code to avoid crash * timeout is now a DP and removed unused method * Update Microsoft.Toolkit.Uwp.UI/Behaviors/FocusBehavior.cs Co-authored-by: Alexandre Zollinger Chohfi <chohfi@outlook.com> Co-authored-by: Alexandre Zollinger Chohfi <alzollin@microsoft.com>
2 parents a07dd51 + bf088a4 commit 6a05a3c

File tree

10 files changed

+383
-1
lines changed

10 files changed

+383
-1
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,15 @@
517517
</Content>
518518
<Content Include="SamplePages\Weibo Service\WeiboCode.bind" />
519519
<Compile Include="Common\TextBlockHyperlinkBehavior.cs" />
520+
<Compile Include="SamplePages\AutoFocusBehavior\AutoFocusBehaviorPage.xaml.cs">
521+
<DependentUpon>AutoFocusBehaviorPage.xaml</DependentUpon>
522+
</Compile>
520523
<Compile Include="SamplePages\EnumValuesExtension\EnumValuesExtensionPage.xaml.cs">
521524
<DependentUpon>EnumValuesExtensionPage.xaml</DependentUpon>
522525
</Compile>
526+
<Compile Include="SamplePages\FocusBehavior\FocusBehaviorPage.xaml.cs">
527+
<DependentUpon>FocusBehaviorPage.xaml</DependentUpon>
528+
</Compile>
523529
<Compile Include="SamplePages\TilesBrush\TilesBrushPage.xaml.cs">
524530
<DependentUpon>TilesBrushPage.xaml</DependentUpon>
525531
</Compile>
@@ -623,6 +629,8 @@
623629
<Content Include="SamplePages\StaggeredLayout\StaggeredLayout.bind" />
624630
<Content Include="SamplePages\EnumValuesExtension\EnumValuesExtensionXaml.bind" />
625631
<Content Include="SamplePages\EnumValuesExtension\EnumValuesExtensionCode.bind" />
632+
<Content Include="SamplePages\FocusBehavior\FocusBehaviorXaml.bind" />
633+
<Content Include="SamplePages\AutoFocusBehavior\AutoFocusBehaviorXaml.bind" />
626634
</ItemGroup>
627635
<ItemGroup>
628636
<Compile Include="App.xaml.cs">
@@ -1001,10 +1009,18 @@
10011009
<SubType>Designer</SubType>
10021010
<Generator>MSBuild:Compile</Generator>
10031011
</Page>
1012+
<Page Include="SamplePages\AutoFocusBehavior\AutoFocusBehaviorPage.xaml">
1013+
<SubType>Designer</SubType>
1014+
<Generator>MSBuild:Compile</Generator>
1015+
</Page>
10041016
<Page Include="SamplePages\EnumValuesExtension\EnumValuesExtensionPage.xaml">
10051017
<Generator>MSBuild:Compile</Generator>
10061018
<SubType>Designer</SubType>
10071019
</Page>
1020+
<Page Include="SamplePages\FocusBehavior\FocusBehaviorPage.xaml">
1021+
<Generator>MSBuild:Compile</Generator>
1022+
<SubType>Designer</SubType>
1023+
</Page>
10081024
<Page Include="SamplePages\TilesBrush\TilesBrushPage.xaml">
10091025
<Generator>MSBuild:Compile</Generator>
10101026
<SubType>Designer</SubType>
@@ -1574,6 +1590,7 @@
15741590
<Name>Visual C++ 2015 Runtime for Universal Windows Platform Apps</Name>
15751591
</SDKReference>
15761592
</ItemGroup>
1593+
<ItemGroup />
15771594
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
15781595
<VisualStudioVersion>14.0</VisualStudioVersion>
15791596
</PropertyGroup>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Page x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.AutoFocusBehaviorPage"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
mc:Ignorable="d">
9+
10+
<Grid>
11+
<StackPanel HorizontalAlignment="Center"
12+
VerticalAlignment="Center">
13+
<Button Content="I receive the focus when loaded">
14+
<interactivity:Interaction.Behaviors>
15+
<behaviors:AutoFocusBehavior />
16+
</interactivity:Interaction.Behaviors>
17+
</Button>
18+
<Button Content="I do not receive the focus when loaded" />
19+
</StackPanel>
20+
</Grid>
21+
</Page>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Windows.UI.Xaml.Controls;
6+
7+
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
8+
{
9+
/// <summary>
10+
/// A page that shows how to use the AutoFocusBehavior
11+
/// </summary>
12+
public sealed partial class AutoFocusBehaviorPage : Page
13+
{
14+
public AutoFocusBehaviorPage() => InitializeComponent();
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
mc:Ignorable="d">
8+
9+
<Grid>
10+
<StackPanel HorizontalAlignment="Center"
11+
VerticalAlignment="Center">
12+
<Button Content="I receive the focus when loaded">
13+
<interactivity:Interaction.Behaviors>
14+
<behaviors:AutoFocusBehavior />
15+
</interactivity:Interaction.Behaviors>
16+
</Button>
17+
<Button Content="I do not receive the focus when loaded" />
18+
</StackPanel>
19+
</Grid>
20+
</Page>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Page x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.FocusBehaviorPage"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
mc:Ignorable="d">
9+
10+
<interactivity:Interaction.Behaviors>
11+
<behaviors:FocusBehavior>
12+
<behaviors:FocusTarget Control="{x:Bind disabledItem}" />
13+
<behaviors:FocusTarget Control="{x:Bind emptyList}" />
14+
<behaviors:FocusTarget Control="{x:Bind enabledItem}" />
15+
</behaviors:FocusBehavior>
16+
</interactivity:Interaction.Behaviors>
17+
18+
<StackPanel>
19+
<Button x:Name="disabledItem"
20+
Content="I'm disabled and cannot get the focus"
21+
IsEnabled="False" />
22+
<ListView x:Name="emptyList">
23+
<!--<ListView.Items>
24+
<x:String>Item 1</x:String>
25+
<x:String>Item 2</x:String>
26+
</ListView.Items>-->
27+
</ListView>
28+
<Button x:Name="enabledItem"
29+
Content="I can get the focus" />
30+
</StackPanel>
31+
</Page>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Windows.UI.Xaml.Controls;
6+
7+
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
8+
{
9+
/// <summary>
10+
/// A page that shows how to use the FocusBehavior
11+
/// </summary>
12+
public sealed partial class FocusBehaviorPage : Page
13+
{
14+
public FocusBehaviorPage() => InitializeComponent();
15+
}
16+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
mc:Ignorable="d">
8+
9+
<interactivity:Interaction.Behaviors>
10+
<behaviors:FocusBehavior>
11+
<behaviors:FocusTarget Control="{x:Bind disabledItem}" />
12+
<behaviors:FocusTarget Control="{x:Bind emptyList}" />
13+
<behaviors:FocusTarget Control="{x:Bind enabledItem}" />
14+
</behaviors:FocusBehavior>
15+
</interactivity:Interaction.Behaviors>
16+
17+
<StackPanel>
18+
<Button x:Name="disabledItem"
19+
Content="I'm disabled and cannot get the focus"
20+
IsEnabled="False" />
21+
<ListView x:Name="emptyList">
22+
<!-- Uncomment this to fill the list and have it receiving the focus
23+
<ListView.Items>
24+
<x:String>Item 1</x:String>
25+
<x:String>Item 2</x:String>
26+
</ListView.Items>-->
27+
</ListView>
28+
<Button x:Name="enabledItem"
29+
Content="I can get the focus" />
30+
</StackPanel>
31+
</Page>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,12 +875,32 @@
875875
"Type": "ViewportBehaviorPage",
876876
"Subcategory": "Systems",
877877
"About": "Behavior for listening element enter or exit viewport",
878-
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI.Animations/ViewportBehavior",
878+
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI/Behaviors",
879879
"CodeFile": "ViewportBehaviorCode.bind",
880880
"XamlCodeFile": "ViewportBehaviorXaml.bind",
881881
"Icon": "/SamplePages/ViewportBehavior/ViewportBehavior.png",
882882
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/animations/ViewportBehavior.md"
883883
},
884+
{
885+
"Name": "AutoFocusBehavior",
886+
"Type": "AutoFocusBehaviorPage",
887+
"Subcategory": "Systems",
888+
"About": "Behavior to automatically set the focus on a control when it loads",
889+
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI/Behaviors/",
890+
"XamlCodeFile": "AutoFocusBehaviorXaml.bind",
891+
"Icon": "/Assets/Helpers.png",
892+
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/AutoFocusBehavior.md"
893+
},
894+
{
895+
"Name": "FocusBehavior",
896+
"Type": "FocusBehaviorPage",
897+
"Subcategory": "Systems",
898+
"About": "Behavior to automatically set the focus on the first control which accepts it",
899+
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI/Behaviors/",
900+
"CodeFile": "FocusBehaviorXaml.bind",
901+
"Icon": "/Assets/Helpers.png",
902+
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/FocusBehavior.md"
903+
},
884904
{
885905
"Name": "Markdown Parser",
886906
"Type": "MarkdownParserPage",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Windows.UI.Xaml;
6+
using Windows.UI.Xaml.Controls;
7+
8+
namespace Microsoft.Toolkit.Uwp.UI.Behaviors
9+
{
10+
/// <summary>
11+
/// This behavior automatically sets the focus on the associated <see cref="Control"/> when it is loaded.
12+
/// </summary>
13+
public sealed class AutoFocusBehavior : BehaviorBase<Control>
14+
{
15+
/// <inheritdoc/>
16+
protected override void OnAssociatedObjectLoaded() => AssociatedObject.Focus(FocusState.Programmatic);
17+
}
18+
}

0 commit comments

Comments
 (0)