Skip to content

Commit

Permalink
Implement the GridView feature by binding directly to the View property.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
koal44 committed Mar 14, 2024
1 parent 0271f7a commit 96d10c0
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
12 changes: 6 additions & 6 deletions src/Wpf.Ui.Gallery/Views/Pages/Collections/ListViewPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
\t</ListView.ItemTemplate>\n
</ListView>
</controls:ControlExample.XamlCode>
<ui:ListView
<ListView
MaxHeight="200"
d:ItemsSource="{d:SampleData ItemCount=2}"
ItemsSource="{Binding ViewModel.BasicListViewItems, Mode=TwoWay}"
Expand All @@ -41,7 +41,7 @@
<TextBlock Margin="8,4" Text="{Binding Name, Mode=OneWay}" />
</DataTemplate>
</ListView.ItemTemplate>
</ui:ListView>
</ListView>
</controls:ControlExample>

<controls:ControlExample Margin="0,36,0,0" HeaderText="ListView with Selection Support.">
Expand All @@ -59,7 +59,7 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:ListView
<ListView
Grid.Column="0"
MaxHeight="200"
d:ItemsSource="{d:SampleData ItemCount=2}"
Expand Down Expand Up @@ -101,7 +101,7 @@
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ui:ListView>
</ListView>
<StackPanel
Grid.Column="1"
MinWidth="120"
Expand Down Expand Up @@ -129,7 +129,7 @@
\t&lt;/ListView.View&gt;\n
&lt;/ListView&gt;
</controls:ControlExample.XamlCode>
<ui:ListView
<ListView
MaxHeight="200"
d:ItemsSource="{d:SampleData ItemCount=3}"
BorderThickness="0"
Expand All @@ -147,7 +147,7 @@
<GridViewColumn DisplayMemberBinding="{Binding Company}" Header="Company" />
</GridView>
</ListView.View>
</ui:ListView>
</ListView>
</controls:ControlExample>
</StackPanel>
</Page>
2 changes: 1 addition & 1 deletion src/Wpf.Ui.SyntaxHighlight/SyntaxHighlight.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<!-- https://github.com/tonsky/FiraCode -->
<FontFamily x:Key="FiraCode">pack://application:,,,/Wpf.Ui.SyntaxHighlight;component/Fonts/#Fira Code</FontFamily>
<FontFamily x:Key="FiraCode">pack://application:,,,/Wpf.Ui;component/Fonts/#Fira Code</FontFamily>

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Wpf.Ui.SyntaxHighlight;component/Controls/CodeBlock.xaml" />
Expand Down
4 changes: 2 additions & 2 deletions src/Wpf.Ui/Controls/AutoSuggestBox/AutoSuggestBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
BorderThickness="1"
CornerRadius="8"
SnapsToDevicePixels="True">
<controls:ListView
<ListView
x:Name="PART_SuggestionsList"
MaxHeight="{TemplateBinding MaxSuggestionListHeight}"
DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
Expand All @@ -162,7 +162,7 @@
VirtualizationMode="Recycling" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</controls:ListView>
</ListView>
</Border>
</Popup>
</Grid>
Expand Down
65 changes: 0 additions & 65 deletions src/Wpf.Ui/Controls/ListView/ListView.cs

This file was deleted.

25 changes: 14 additions & 11 deletions src/Wpf.Ui/Controls/ListView/ListView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Wpf.Ui.Controls">
xmlns:controls="clr-namespace:Wpf.Ui.Controls"
xmlns:converters="clr-namespace:Wpf.Ui.Converters">

<ControlTemplate x:Key="NullViewTemplate" TargetType="{x:Type controls:ListView}">
<converters:ViewToListViewViewStateConverter x:Key="ViewToListViewViewStateConverter" />

<ControlTemplate x:Key="NullViewTemplate" TargetType="{x:Type ListView}">
<Grid>
<controls:PassiveScrollViewer
x:Name="PART_ContentHost"
Expand Down Expand Up @@ -134,7 +137,7 @@
</Setter>
</Style>

<ControlTemplate x:Key="GridViewTemplate" TargetType="{x:Type controls:ListView}">
<ControlTemplate x:Key="GridViewTemplate" TargetType="{x:Type ListView}">
<Border
Name="Bd"
Background="Transparent"
Expand All @@ -155,18 +158,18 @@
</ControlTemplate.Triggers>
</ControlTemplate>

<Style x:Key="ListViewStyle" TargetType="{x:Type controls:ListView}">
<Style x:Key="ListViewStyle" TargetType="{x:Type ListView}">
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.CanContentScroll" Value="True" />
<Setter Property="Control.VerticalContentAlignment" Value="Center" />
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="VirtualizingPanel.IsVirtualizing" Value="True" />
<Setter Property="VirtualizingPanel.VirtualizationMode" Value="Standard" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ViewState, RelativeSource={RelativeSource Mode=Self}}" Value="{x:Static controls:ListViewViewState.Default}">
<DataTrigger Binding="{Binding Path=View, RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource ViewToListViewViewStateConverter}}" Value="{x:Static controls:ListViewViewState.Default}">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
Expand All @@ -176,12 +179,12 @@
</Setter>
<Setter Property="Template" Value="{DynamicResource NullViewTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=ViewState, RelativeSource={RelativeSource Mode=Self}}" Value="{x:Static controls:ListViewViewState.GridView}">
<DataTrigger Binding="{Binding Path=View, RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource ViewToListViewViewStateConverter}}" Value="{x:Static controls:ListViewViewState.GridView}">
<Setter Property="Template" Value="{DynamicResource GridViewTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>

<Style BasedOn="{StaticResource ListViewStyle}" TargetType="{x:Type controls:ListView}" />
<Style BasedOn="{StaticResource ListViewStyle}" TargetType="{x:Type ListView}" />

</ResourceDictionary>
12 changes: 9 additions & 3 deletions src/Wpf.Ui/Controls/ListView/ListViewItem.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
All Rights Reserved.
-->

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Wpf.Ui.Controls"
xmlns:converters="clr-namespace:Wpf.Ui.Converters">

<converters:ViewToListViewViewStateConverter x:Key="ViewToListViewViewStateConverter" />

<ControlTemplate x:Key="NullViewItemTemplate" TargetType="{x:Type ListViewItem}">
<Border
Expand Down Expand Up @@ -95,10 +101,10 @@
<Setter Property="Margin" Value="0,0,0,2" />
<Setter Property="Padding" Value="4" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ViewState, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" Value="Default">
<DataTrigger Binding="{Binding Path=View, RelativeSource={RelativeSource AncestorType={x:Type ListView}}, Converter={StaticResource ViewToListViewViewStateConverter}}" Value="{x:Static controls:ListViewViewState.Default}">
<Setter Property="Template" Value="{DynamicResource NullViewItemTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=ViewState, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" Value="GridView">
<DataTrigger Binding="{Binding Path=View, RelativeSource={RelativeSource AncestorType={x:Type ListView}}, Converter={StaticResource ViewToListViewViewStateConverter}}" Value="{x:Static controls:ListViewViewState.GridView}">
<Setter Property="Template" Value="{DynamicResource GridViewItemTemplate}" />
</DataTrigger>
</Style.Triggers>
Expand Down
28 changes: 28 additions & 0 deletions src/Wpf.Ui/Converters/ViewToListViewViewStateConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System.Windows.Controls;
using System.Windows.Data;
using Wpf.Ui.Controls;

namespace Wpf.Ui.Converters;

internal class ViewToListViewViewStateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value switch
{
GridView => ListViewViewState.GridView,
null => ListViewViewState.Default,
_ => ListViewViewState.Default
};
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("Converting from ListViewViewState to View is not supported.");
}
}

0 comments on commit 96d10c0

Please sign in to comment.