From f055c4c74226ec06a4a3ed75f76232ebb357051c Mon Sep 17 00:00:00 2001 From: koal44 Date: Tue, 27 Feb 2024 15:38:50 -0800 Subject: [PATCH 1/5] Fix ListView to be compatible when ListView.View is a GridView --- .../ViewModels/Windows/MainWindowViewModel.cs | 2 +- .../Views/Pages/Collections/ListViewPage.xaml | 49 +++- .../AutoSuggestBox/AutoSuggestBox.xaml | 4 +- src/Wpf.Ui/Controls/ListView/ListView.cs | 60 +++++ src/Wpf.Ui/Controls/ListView/ListView.xaml | 217 ++++++++++++++---- .../Controls/ListView/ListViewItem.xaml | 142 ++++++++---- 6 files changed, 367 insertions(+), 107 deletions(-) create mode 100644 src/Wpf.Ui/Controls/ListView/ListView.cs diff --git a/src/Wpf.Ui.Gallery/ViewModels/Windows/MainWindowViewModel.cs b/src/Wpf.Ui.Gallery/ViewModels/Windows/MainWindowViewModel.cs index 7e96e95c5..43531e71d 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(ListView), typeof(ListViewPage)), + new NavigationViewItem(nameof(Ui.Controls.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 345d4b568..7ce461c3d 100644 --- a/src/Wpf.Ui.Gallery/Views/Pages/Collections/ListViewPage.xaml +++ b/src/Wpf.Ui.Gallery/Views/Pages/Collections/ListViewPage.xaml @@ -12,7 +12,7 @@ Title="ListViewPage" d:DataContext="{d:DesignInstance local:ListViewPage, IsDesignTimeCreatable=False}" - d:DesignHeight="450" + d:DesignHeight="750" d:DesignWidth="800" ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}" ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}" @@ -30,8 +30,9 @@ \t</ListView.ItemTemplate>\n </ListView> - @@ -40,7 +41,7 @@ - + @@ -58,9 +59,10 @@ - @@ -99,7 +101,7 @@ - + + + + + <ListView ItemsSource="{Binding ViewModel.BasicListViewItems}">\n + \t<ListView.View>\n + \t\t<GridView>\n + \t\t\t<GridViewColumn DisplayMemberBinding="{Binding FirstName}" Header="First Name"/>\n + \t\t\t<GridViewColumn DisplayMemberBinding="{Binding LastName}" Header="Last Name"/>\n + \t\t\t<GridViewColumn DisplayMemberBinding="{Binding Company}" Header="Company"/>\n + \t\t</GridView>\n + \t</ListView.View>\n + </ListView> + + + + + + + + + + + diff --git a/src/Wpf.Ui/Controls/AutoSuggestBox/AutoSuggestBox.xaml b/src/Wpf.Ui/Controls/AutoSuggestBox/AutoSuggestBox.xaml index d6c8aa11a..ec3330535 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 new file mode 100644 index 000000000..5c7c422f6 --- /dev/null +++ b/src/Wpf.Ui/Controls/ListView/ListView.cs @@ -0,0 +1,60 @@ +namespace Wpf.Ui.Controls; + +public class ListView : System.Windows.Controls.ListView +{ + public string ViewState + { + get => (string)GetValue(ViewStateProperty); + set => SetValue(ViewStateProperty, value); + } + + /// Identifies the dependency property. + public static readonly DependencyProperty ViewStateProperty = DependencyProperty.Register(nameof(ViewState), typeof(string), typeof(ListView), new FrameworkPropertyMetadata("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() + { + var viewState = View is null ? "Default" : "GridView"; + 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 bef8c8cb2..90c506ac9 100644 --- a/src/Wpf.Ui/Controls/ListView/ListView.xaml +++ b/src/Wpf.Ui/Controls/ListView/ListView.xaml @@ -10,63 +10,178 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:Wpf.Ui.Controls"> - - + + - - + - - - + - -