From 96d10c09cc79d56cddd711f232a97ca4d910f172 Mon Sep 17 00:00:00 2001 From: koal44 Date: Thu, 14 Mar 2024 04:45:44 -0700 Subject: [PATCH] Implement the GridView feature by binding directly to the View property. Removed the custom class ui:ListView as its purpose was to expose the ViewState, which can be simplified by directly binding to ListView.View with a ValueConverter. --- .../ViewModels/Windows/MainWindowViewModel.cs | 2 +- .../Views/Pages/Collections/ListViewPage.xaml | 12 ++-- .../SyntaxHighlight.xaml | 2 +- .../AutoSuggestBox/AutoSuggestBox.xaml | 4 +- src/Wpf.Ui/Controls/ListView/ListView.cs | 65 ------------------- src/Wpf.Ui/Controls/ListView/ListView.xaml | 25 +++---- .../Controls/ListView/ListViewItem.xaml | 12 +++- .../ViewToListViewViewStateConverter.cs | 28 ++++++++ 8 files changed, 61 insertions(+), 89 deletions(-) delete mode 100644 src/Wpf.Ui/Controls/ListView/ListView.cs create mode 100644 src/Wpf.Ui/Converters/ViewToListViewViewStateConverter.cs diff --git a/src/Wpf.Ui.Gallery/ViewModels/Windows/MainWindowViewModel.cs b/src/Wpf.Ui.Gallery/ViewModels/Windows/MainWindowViewModel.cs index 43531e71d..7e96e95c5 100644 --- a/src/Wpf.Ui.Gallery/ViewModels/Windows/MainWindowViewModel.cs +++ b/src/Wpf.Ui.Gallery/ViewModels/Windows/MainWindowViewModel.cs @@ -72,7 +72,7 @@ public partial class MainWindowViewModel : ObservableObject { new NavigationViewItem(nameof(System.Windows.Controls.DataGrid), typeof(DataGridPage)), new NavigationViewItem(nameof(ListBox), typeof(ListBoxPage)), - new NavigationViewItem(nameof(Ui.Controls.ListView), typeof(ListViewPage)), + new NavigationViewItem(nameof(ListView), typeof(ListViewPage)), new NavigationViewItem(nameof(TreeView), typeof(TreeViewPage)), #if DEBUG new NavigationViewItem("TreeList", typeof(TreeListPage)), diff --git a/src/Wpf.Ui.Gallery/Views/Pages/Collections/ListViewPage.xaml b/src/Wpf.Ui.Gallery/Views/Pages/Collections/ListViewPage.xaml index 7ce461c3d..a9a6f6e48 100644 --- a/src/Wpf.Ui.Gallery/Views/Pages/Collections/ListViewPage.xaml +++ b/src/Wpf.Ui.Gallery/Views/Pages/Collections/ListViewPage.xaml @@ -30,7 +30,7 @@ \t</ListView.ItemTemplate>\n </ListView> - - + @@ -59,7 +59,7 @@ - - + - - + diff --git a/src/Wpf.Ui.SyntaxHighlight/SyntaxHighlight.xaml b/src/Wpf.Ui.SyntaxHighlight/SyntaxHighlight.xaml index 88d284dcf..279e1d3ef 100644 --- a/src/Wpf.Ui.SyntaxHighlight/SyntaxHighlight.xaml +++ b/src/Wpf.Ui.SyntaxHighlight/SyntaxHighlight.xaml @@ -8,7 +8,7 @@ - pack://application:,,,/Wpf.Ui.SyntaxHighlight;component/Fonts/#Fira Code + pack://application:,,,/Wpf.Ui;component/Fonts/#Fira Code diff --git a/src/Wpf.Ui/Controls/AutoSuggestBox/AutoSuggestBox.xaml b/src/Wpf.Ui/Controls/AutoSuggestBox/AutoSuggestBox.xaml index ec3330535..d6c8aa11a 100644 --- a/src/Wpf.Ui/Controls/AutoSuggestBox/AutoSuggestBox.xaml +++ b/src/Wpf.Ui/Controls/AutoSuggestBox/AutoSuggestBox.xaml @@ -144,7 +144,7 @@ BorderThickness="1" CornerRadius="8" SnapsToDevicePixels="True"> - - + diff --git a/src/Wpf.Ui/Controls/ListView/ListView.cs b/src/Wpf.Ui/Controls/ListView/ListView.cs deleted file mode 100644 index 44efcd2a9..000000000 --- a/src/Wpf.Ui/Controls/ListView/ListView.cs +++ /dev/null @@ -1,65 +0,0 @@ -namespace Wpf.Ui.Controls; - -public class ListView : System.Windows.Controls.ListView -{ - public ListViewViewState ViewState - { - get => (ListViewViewState)GetValue(ViewStateProperty); - set => SetValue(ViewStateProperty, value); - } - - /// Identifies the dependency property. - public static readonly DependencyProperty ViewStateProperty = DependencyProperty.Register(nameof(ViewState), typeof(ListViewViewState), typeof(ListView), new FrameworkPropertyMetadata(ListViewViewState.Default, OnViewStateChanged)); - - private static void OnViewStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - if (d is not ListView self) - { - return; - } - - self.OnViewStateChanged(e); - } - - protected virtual void OnViewStateChanged(DependencyPropertyChangedEventArgs e) - { - // derived classes can hook `ViewState` property changes by overriding this method - } - - public ListView() - { - Loaded += OnLoaded; - } - - private void OnLoaded(object sender, RoutedEventArgs e) - { - // immediately unsubscribe to prevent memory leaks - Loaded -= OnLoaded; - - // get the descriptor for the `View` property since the framework doesn't provide a public hook for it - var descriptor = DependencyPropertyDescriptor.FromProperty(System.Windows.Controls.ListView.ViewProperty, typeof(System.Windows.Controls.ListView)); - descriptor?.AddValueChanged(this, OnViewPropertyChanged); - UpdateViewState(); // set the initial state - } - - private void OnViewPropertyChanged(object? sender, EventArgs e) - { - UpdateViewState(); - } - - private void UpdateViewState() - { - ListViewViewState viewState = View switch - { - System.Windows.Controls.GridView => ListViewViewState.GridView, - null => ListViewViewState.Default, - _ => ListViewViewState.Default - }; - SetCurrentValue(ViewStateProperty, viewState); - } - - static ListView() - { - DefaultStyleKeyProperty.OverrideMetadata(typeof(ListView), new FrameworkPropertyMetadata(typeof(ListView))); - } -} diff --git a/src/Wpf.Ui/Controls/ListView/ListView.xaml b/src/Wpf.Ui/Controls/ListView/ListView.xaml index 238a25c9c..3cc76c08d 100644 --- a/src/Wpf.Ui/Controls/ListView/ListView.xaml +++ b/src/Wpf.Ui/Controls/ListView/ListView.xaml @@ -8,9 +8,12 @@ + xmlns:controls="clr-namespace:Wpf.Ui.Controls" + xmlns:converters="clr-namespace:Wpf.Ui.Converters"> - + + + - + - -