-
Notifications
You must be signed in to change notification settings - Fork 751
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6371 from unoplatform/dev/xygu/20210630/selection…
…-dp-event-ordering fix(Selector): firing order of data-bound dp & event
- Loading branch information
Showing
9 changed files
with
412 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...amplesApp/UITests.Shared/Windows_UI_Xaml_Controls/ListView/ListView_Selection_Events.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<Page x:Class="UITests.Windows_UI_Xaml_Controls.ListView.ListView_Selection_Events" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="using:UITests.Windows_UI_Xaml_Controls.ListView" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
mc:Ignorable="d" | ||
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> | ||
|
||
<Grid> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="1*" /> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="1*" /> | ||
<RowDefinition Height="Auto" /> | ||
</Grid.RowDefinitions> | ||
|
||
<TextBlock Grid.Row="0" | ||
Text="Item for selection:" /> | ||
<ListView Grid.Row="1" | ||
x:Name="SampleListView" | ||
ItemsSource="{x:Bind ViewModel.Source}" | ||
SelectedIndex="{x:Bind ViewModel.SelectedIndex, Mode=TwoWay}" | ||
SelectedItem="{x:Bind ViewModel.SelectedItem, Mode=TwoWay}" | ||
SelectedValue="{x:Bind ViewModel.SelectedValue, Mode=TwoWay}" | ||
SelectionMode="Single" | ||
ScrollViewer.VerticalScrollBarVisibility="Auto"> | ||
<ListView.ItemTemplate> | ||
<DataTemplate> | ||
<TextBlock AutomationProperties.AutomationId="{Binding}" | ||
Text="{Binding}" /> | ||
</DataTemplate> | ||
</ListView.ItemTemplate> | ||
</ListView> | ||
|
||
<TextBlock Grid.Row="2" | ||
Text="Event Logs:" /> | ||
<!--<ListView Grid.Row="3" | ||
x:Name="LogListView"> | ||
<ListView.ItemTemplate> | ||
<DataTemplate> | ||
<TextBlock Text="{Binding}" | ||
TextWrapping="Wrap" /> | ||
</DataTemplate> | ||
</ListView.ItemTemplate> | ||
</ListView>--> | ||
<ScrollViewer Grid.Row="3" | ||
HorizontalScrollBarVisibility="Disabled" | ||
VerticalScrollBarVisibility="Auto"> | ||
<TextBox x:Name="EventLogs" | ||
IsReadOnly="True" | ||
AcceptsReturn="True" | ||
TextWrapping="Wrap" /> | ||
</ScrollViewer> | ||
|
||
|
||
<StackPanel Grid.Row="4"> | ||
<Button x:Name="SetSelectIndexTo0Button" | ||
Content="Set SelectionIndex to 0" | ||
Click="SetSelectIndexTo0" /> | ||
<Button x:Name="ClearLogsButton" | ||
Content="Clear Logs" | ||
Click="ClearLogs" /> | ||
</StackPanel> | ||
</Grid> | ||
</Page> |
107 changes: 107 additions & 0 deletions
107
...lesApp/UITests.Shared/Windows_UI_Xaml_Controls/ListView/ListView_Selection_Events.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.InteropServices.WindowsRuntime; | ||
using Windows.Foundation; | ||
using Windows.Foundation.Collections; | ||
using Windows.UI.Xaml; | ||
using Uno.UI.Samples.Controls; | ||
using Windows.UI.Xaml.Controls; | ||
using Windows.UI.Xaml.Controls.Primitives; | ||
using Windows.UI.Xaml.Data; | ||
using Windows.UI.Xaml.Input; | ||
using Windows.UI.Xaml.Media; | ||
using Windows.UI.Xaml.Navigation; | ||
using System.ComponentModel; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace UITests.Windows_UI_Xaml_Controls.ListView | ||
{ | ||
[SampleControlInfo("ListView", nameof(ListView_Selection_Events), description: "The sequence of events when selecting an item should happen in the same order when compared to uwp.")] | ||
public sealed partial class ListView_Selection_Events : Page | ||
{ | ||
private CustomViewModel ViewModel { get; } = new CustomViewModel(); | ||
|
||
public ListView_Selection_Events() | ||
{ | ||
this.InitializeComponent(); | ||
SampleListView.SelectionChanged += (s, e) => AddLog( | ||
"LV.SelectionChanged: (Item|Value|Index): " + | ||
$"\n\t- lv:({Format(SampleListView.SelectedItem)}|{Format(SampleListView.SelectedValue)}|{Format(SampleListView.SelectedIndex)}), " + | ||
$"\n\t- vm:({Format(ViewModel.SelectedItem)}|{Format(ViewModel.SelectedValue)}|{Format(ViewModel.SelectedIndex)})" | ||
); | ||
ViewModel.PropertyChanged += (s, e) => | ||
{ | ||
AddLog($"VM.PropertyChanged: [{e.PropertyName}]->{Format(GetValueFrom(e.PropertyName))}"); | ||
|
||
object GetValueFrom(string propertyName) => propertyName switch | ||
{ | ||
nameof(CustomViewModel.SelectedItem) => ViewModel.SelectedItem, | ||
nameof(CustomViewModel.SelectedValue) => ViewModel.SelectedValue, | ||
nameof(CustomViewModel.SelectedIndex) => ViewModel.SelectedIndex, | ||
|
||
_ => throw new ArgumentOutOfRangeException(propertyName), | ||
}; | ||
}; | ||
|
||
string Format(object value) => value?.ToString() ?? "<null>"; | ||
void AddLog(string log) | ||
{ | ||
//LogListView.Items.Add(log); | ||
EventLogs.Text += (string.IsNullOrEmpty(EventLogs.Text) ? null : "\n") + log; | ||
} | ||
} | ||
private void SetSelectIndexTo0(object sender, RoutedEventArgs e) | ||
{ | ||
SampleListView.SelectedIndex = 0; | ||
} | ||
private void ClearLogs(object sender, RoutedEventArgs e) | ||
{ | ||
//LogListView.Items.Clear(); | ||
EventLogs.Text = string.Empty; | ||
} | ||
|
||
// not using ViewModelBase because it would fire the same event twice, once as $"{propertyName}" and once as $"Item[{propertyName}]"... | ||
private class CustomViewModel : global::System.ComponentModel.INotifyPropertyChanged | ||
{ | ||
public event global::System.ComponentModel.PropertyChangedEventHandler PropertyChanged; | ||
|
||
public string[] Source { get; } = Enumerable.Range(0, 5).Select(x => $"Item_{x}").ToArray(); | ||
|
||
#region SelectedItem | ||
private object _selectedItem; | ||
public object SelectedItem | ||
{ | ||
get => _selectedItem; | ||
set => RaiseAndSetIfChanged(ref _selectedItem, value); | ||
} | ||
#endregion | ||
#region SelectedValue | ||
private object _selectedValue; | ||
public object SelectedValue | ||
{ | ||
get => _selectedValue; | ||
set => RaiseAndSetIfChanged(ref _selectedValue, value); | ||
} | ||
#endregion | ||
#region SelectedIndex | ||
private int _selectedIndex; | ||
public int SelectedIndex | ||
{ | ||
get => _selectedIndex; | ||
set => RaiseAndSetIfChanged(ref _selectedIndex, value); | ||
} | ||
#endregion | ||
|
||
protected void RaiseAndSetIfChanged<T>(ref T backingField, T value, [CallerMemberName] string propertyName = null) | ||
{ | ||
if (!EqualityComparer<T>.Default.Equals(backingField, value)) | ||
{ | ||
backingField = value; | ||
PropertyChanged?.Invoke(this, new global::System.ComponentModel.PropertyChangedEventArgs(propertyName)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.