[FZ Editor] Custom button with automation event on click (#12338)

* Custom button with automation event on click

* Rename MyButton to ClickAutomationEventButton

* Rename property to OnClickAutomationValue

* Remove unneeded line
This commit is contained in:
Stefan Markovic
2021-07-26 14:24:15 +02:00
committed by GitHub
parent 9af08d9842
commit 6dfaf6a21c
6 changed files with 131 additions and 4 deletions

View File

@@ -2,7 +2,8 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FancyZonesEditor"
mc:Ignorable="d" mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"> d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="Body"> <Grid x:Name="Body">

View File

@@ -34,8 +34,7 @@
<StackPanel Margin="16" <StackPanel Margin="16"
FocusManager.FocusedElement="{Binding ElementName=newZoneButton}"> FocusManager.FocusedElement="{Binding ElementName=newZoneButton}">
<Button x:Name="newZoneButton" <local:ClickAutomationEventButton x:Name="newZoneButton"
AutomationProperties.LabeledBy="{Binding ElementName=newZoneName}"
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
Height="64" Height="64"
Width="64" Width="64"
@@ -49,7 +48,8 @@
ToolTip="{x:Static props:Resources.Add_zone}" ToolTip="{x:Static props:Resources.Add_zone}"
DataContext="{Binding Path=Model, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" DataContext="{Binding Path=Model, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
IsEnabled="{Binding IsZoneAddingAllowed}" IsEnabled="{Binding IsZoneAddingAllowed}"
Click="OnAddZone" /> Click="OnAddZone"
OnClickAutomationValue="{x:Static props:Resources.New_zone_added}" />
<Grid Margin="0,24,0,0"> <Grid Margin="0,24,0,0">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>

View File

@@ -0,0 +1,9 @@
<Button x:Class="FancyZonesEditor.ClickAutomationEventButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FancyZonesEditor"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
</Button>

View File

@@ -0,0 +1,105 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
namespace FancyZonesEditor
{
public partial class ClickAutomationEventButton : Button
{
public ClickAutomationEventButton()
: base()
{
InitializeComponent();
Click += OnClick;
}
public string OnClickAutomationValue
{
get { return (string)GetValue(OnClickAutomationValueProperty); }
set { SetValue(OnClickAutomationValueProperty, value); }
}
public static readonly DependencyProperty OnClickAutomationValueProperty =
DependencyProperty.Register(
"Value", typeof(string), typeof(ClickAutomationEventButton));
private void OnClick(object sender, RoutedEventArgs e)
{
if (AutomationPeer.ListenerExists(AutomationEvents.PropertyChanged))
{
ClickAutomationEventButtonAutomationPeer peer =
UIElementAutomationPeer.FromElement(this) as ClickAutomationEventButtonAutomationPeer;
if (peer != null)
{
peer.RaisePropertyChangedEvent(
ValuePatternIdentifiers.ValueProperty,
null,
OnClickAutomationValue);
}
}
}
protected override AutomationPeer OnCreateAutomationPeer()
{
return new ClickAutomationEventButtonAutomationPeer(this);
}
public class ClickAutomationEventButtonAutomationPeer : FrameworkElementAutomationPeer, IValueProvider
{
public ClickAutomationEventButtonAutomationPeer(ClickAutomationEventButton control)
: base(control)
{
}
protected override string GetClassNameCore()
{
return nameof(ClickAutomationEventButton);
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Button;
}
public override object GetPattern(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.Value)
{
return this;
}
return base.GetPattern(patternInterface);
}
public void SetValue(string value)
{
MyOwner.OnClickAutomationValue = value;
}
private ClickAutomationEventButton MyOwner
{
get
{
return (ClickAutomationEventButton)Owner;
}
}
public string Value
{
get { return MyOwner.OnClickAutomationValue; }
}
public bool IsReadOnly
{
get { return !IsEnabled(); }
}
}
}
}

View File

@@ -528,6 +528,15 @@ namespace FancyZonesEditor.Properties {
} }
} }
/// <summary>
/// Looks up a localized string similar to New zone added.
/// </summary>
public static string New_zone_added {
get {
return ResourceManager.GetString("New_zone_added", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to Create or duplicate a layout to get started. /// Looks up a localized string similar to Create or duplicate a layout to get started.
/// </summary> /// </summary>

View File

@@ -120,6 +120,9 @@
<data name="Add_zone" xml:space="preserve"> <data name="Add_zone" xml:space="preserve">
<value>Add new zone</value> <value>Add new zone</value>
</data> </data>
<data name="New_zone_added" xml:space="preserve">
<value>New zone added</value>
</data>
<data name="Apply" xml:space="preserve"> <data name="Apply" xml:space="preserve">
<value>Apply</value> <value>Apply</value>
</data> </data>