Skip to content

Commit

Permalink
Merge pull request #317 from microsoft/DataBindingDemo-AccessibilityF…
Browse files Browse the repository at this point in the history
…ixes

Data binding demo accessibility fixes
  • Loading branch information
MarkDHarper authored Oct 21, 2020
2 parents e23a284 + 62ac489 commit 9b441a3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
20 changes: 19 additions & 1 deletion Sample Applications/DataBindingDemo/AddProductWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

using System;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Controls;

namespace DataBindingDemo
{
Expand All @@ -29,5 +32,20 @@ private void SubmitProduct(object sender, RoutedEventArgs e)
((App) Application.Current).AuctionItems.Add(item);
Close();
}

private void OnValidationError(object sender, ValidationErrorEventArgs e)
{
// Get the current UIA ItemStatus from the element element.
var oldStatus = AutomationProperties.GetItemStatus((DependencyObject)sender);

// Set some sample new ItemStatus here...
var newStatus = e.Action == ValidationErrorEventAction.Added ? e.Error.ErrorContent.ToString() : String.Empty;
AutomationProperties.SetItemStatus((DependencyObject)sender, newStatus);

// Having just set the new ItemStatus, raise a UIA property changed event. Note that the peer may
// be null here unless a UIA client app such as Narrator or the AccEvent SDK tool are running.
var automationPeer = UIElementAutomationPeer.FromElement((UIElement)sender);
automationPeer?.RaisePropertyChangedEvent(AutomationElementIdentifiers.ItemStatusProperty, oldStatus, newStatus);
}
}
}
}
11 changes: 8 additions & 3 deletions Sample Applications/DataBindingDemo/AddProductWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@
<TextBlock Grid.Row="2" Grid.Column="0" Style="{StaticResource SmallTitleStyle}" Margin="0,5,0,5">Start Price:</TextBlock>

<TextBox Name="StartPriceEntryForm" AutomationProperties.Name="Start Price" Grid.Row="2" Grid.Column="1"
Style="{StaticResource TextStyleTextBox}" Margin="8,5,0,5">
Style="{StaticResource TextStyleTextBox}" Margin="8,5,0,5"
Validation.Error="OnValidationError">
<TextBox.Text>
<Binding Path="StartPrice" UpdateSourceTrigger="PropertyChanged">
<Binding Path="StartPrice" UpdateSourceTrigger="PropertyChanged"
NotifyOnValidationError="True">
<Binding.ValidationRules>
<ExceptionValidationRule />
</Binding.ValidationRules>
Expand All @@ -71,9 +73,12 @@

<TextBox Name="StartDateEntryForm" AutomationProperties.Name="Start Date" Grid.Row="3" Grid.Column="1"
Validation.ErrorTemplate="{StaticResource ValidationTemplate}"
Style="{StaticResource TextStyleTextBox}" Margin="8,5,0,5">
Style="{StaticResource TextStyleTextBox}" Margin="8,5,0,5"
Validation.Error="OnValidationError"
AutomationProperties.LiveSetting="Assertive">
<TextBox.Text>
<Binding Path="StartDate" UpdateSourceTrigger="PropertyChanged"
NotifyOnValidationError="True"
Converter="{StaticResource DateConverter}">
<Binding.ValidationRules>
<local:FutureDateRule />
Expand Down
2 changes: 1 addition & 1 deletion Sample Applications/DataBindingDemo/AuctionItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public int StartPrice
{
if (value < 0)
{
throw new ArgumentException("Price must be positive");
throw new ArgumentException("Price must be positive. Provide a positive price");
}
_startPrice = value;
OnPropertyChanged("StartPrice");
Expand Down
7 changes: 4 additions & 3 deletions Sample Applications/DataBindingDemo/FutureDateRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
}
catch (FormatException)
{
return new ValidationResult(false, "Value is not a valid date.");
return new ValidationResult(false, "Value is not a valid date. Please enter a valid date");
}
if (DateTime.Now.Date > date)
{
return new ValidationResult(false, "Please enter a date in the future.");
return new ValidationResult(false, "Value is not a future date. Please enter a date in the future.");
}
return ValidationResult.ValidResult;
}

}
}
}
20 changes: 19 additions & 1 deletion Sample Applications/DataBindingDemo/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// // Copyright (c) Microsoft. All rights reserved.
// // Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Data;

namespace DataBindingDemo
Expand Down Expand Up @@ -41,11 +44,21 @@ private void AddGrouping(object sender, RoutedEventArgs args)
// This groups the items in the view by the property "Category"
var groupDescription = new PropertyGroupDescription {PropertyName = "Category"};
_listingDataView.GroupDescriptions.Add(groupDescription);

NotifyUpdate();

}

private void NotifyUpdate()
{
var listingPeer = ListBoxAutomationPeer.FromElement(Master);
listingPeer.RaiseAutomationEvent(AutomationEvents.LiveRegionChanged);
}

private void RemoveGrouping(object sender, RoutedEventArgs args)
{
_listingDataView.GroupDescriptions.Clear();
NotifyUpdate();
}

private void AddSorting(object sender, RoutedEventArgs args)
Expand All @@ -57,21 +70,26 @@ private void AddSorting(object sender, RoutedEventArgs args)
new SortDescription("Category", ListSortDirection.Ascending));
_listingDataView.SortDescriptions.Add(
new SortDescription("StartDate", ListSortDirection.Ascending));
NotifyUpdate();
}

private void RemoveSorting(object sender, RoutedEventArgs args)
{
_listingDataView.SortDescriptions.Clear();
NotifyUpdate();
}

private void AddFiltering(object sender, RoutedEventArgs args)
{
_listingDataView.Filter += ShowOnlyBargainsFilter;
NotifyUpdate();
}

private void RemoveFiltering(object sender, RoutedEventArgs args)
{
_listingDataView.Filter -= ShowOnlyBargainsFilter;
NotifyUpdate();
}
}
}

}
6 changes: 4 additions & 2 deletions Sample Applications/DataBindingDemo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@
</CheckBox>


<ListBox Name="Master" AutomationProperties.Name="Master" Grid.Row="2" Grid.ColumnSpan="3" Margin="8"
ItemsSource="{Binding Source={StaticResource ListingDataView}}">
<ListBox Name="Master" AutomationProperties.Name="List of Items For Sale" Grid.Row="2" Grid.ColumnSpan="3" Margin="8"
ItemsSource="{Binding Source={StaticResource ListingDataView}}"
AutomationProperties.LiveSetting="Assertive">
<ListBox.GroupStyle>
<GroupStyle
HeaderTemplate="{StaticResource GroupingHeaderTemplate}" />
Expand All @@ -68,5 +69,6 @@
<Button Name="OpenAddProduct" Grid.Row="4" Grid.Column="1" Content="Add Product" HorizontalAlignment="Center"
Margin="8"
Click="OpenAddProductWindow" />

</Grid>
</Window>

0 comments on commit 9b441a3

Please sign in to comment.