Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(ListView), typeof(ListViewPage)),
new NavigationViewItem(nameof(Ui.Controls.ListView), typeof(ListViewPage)),
new NavigationViewItem(nameof(TreeView), typeof(TreeViewPage)),
#if DEBUG
new NavigationViewItem("TreeList", typeof(TreeListPage)),
Expand Down
57 changes: 46 additions & 11 deletions src/Wpf.Ui.Gallery/Views/Pages/Collections/ListViewPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -30,17 +30,18 @@
\t</ListView.ItemTemplate>\n
</ListView>
</controls:ControlExample.XamlCode>
<ListView
Height="200"
<ui:ListView
MaxHeight="200"
d:ItemsSource="{d:SampleData ItemCount=2}"
ItemsSource="{Binding ViewModel.BasicListViewItems, Mode=TwoWay}"
SelectedIndex="2"
SelectionMode="Single">
<ListView.ItemTemplate>
<ui:ListView.ItemTemplate>
<DataTemplate DataType="{x:Type models:Person}">
<TextBlock Margin="8,4" Text="{Binding Name, Mode=OneWay}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ui:ListView.ItemTemplate>
</ui:ListView>
</controls:ControlExample>

<controls:ControlExample Margin="0,36,0,0" HeaderText="ListView with Selection Support.">
Expand All @@ -58,13 +59,14 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ListView
<ui:ListView
Grid.Column="0"
Height="200"
MaxHeight="200"
d:ItemsSource="{d:SampleData ItemCount=2}"
ItemsSource="{Binding ViewModel.BasicListViewItems, Mode=TwoWay}"
SelectedIndex="1"
SelectionMode="{Binding ViewModel.ListViewSelectionMode, Mode=OneWay}">
<ListView.ItemTemplate>
<ui:ListView.ItemTemplate>
<DataTemplate DataType="{x:Type models:Person}">
<Grid Margin="8,0">
<Grid.RowDefinitions>
Expand Down Expand Up @@ -98,8 +100,8 @@
Text="{Binding Company, Mode=OneWay}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ui:ListView.ItemTemplate>
</ui:ListView>
<StackPanel
Grid.Column="1"
MinWidth="120"
Expand All @@ -114,5 +116,38 @@
</StackPanel>
</Grid>
</controls:ControlExample>

<controls:ControlExample Margin="0,36,0,0" HeaderText="ListView with GridView">
<controls:ControlExample.XamlCode>
&lt;ListView ItemsSource=&quot;{Binding ViewModel.BasicListViewItems}&quot;&gt;\n
\t&lt;ListView.View&gt;\n
\t\t&lt;GridView&gt;\n
\t\t\t&lt;GridViewColumn DisplayMemberBinding=&quot;{Binding FirstName}&quot; Header=&quot;First Name&quot;/&gt;\n
\t\t\t&lt;GridViewColumn DisplayMemberBinding=&quot;{Binding LastName}&quot; Header=&quot;Last Name&quot;/&gt;\n
\t\t\t&lt;GridViewColumn DisplayMemberBinding=&quot;{Binding Company}&quot; Header=&quot;Company&quot;/&gt;\n
\t\t&lt;/GridView&gt;\n
\t&lt;/ListView.View&gt;\n
&lt;/ListView&gt;
</controls:ControlExample.XamlCode>
<ui:ListView
MaxHeight="200"
d:ItemsSource="{d:SampleData ItemCount=3}"
BorderThickness="0"
ItemsSource="{Binding ViewModel.BasicListViewItems, Mode=TwoWay}">
<ui:ListView.View>
<GridView>
<GridViewColumn
Width="100"
DisplayMemberBinding="{Binding FirstName}"
Header="First Name" />
<GridViewColumn
Width="100"
DisplayMemberBinding="{Binding LastName}"
Header="Last Name" />
<GridViewColumn DisplayMemberBinding="{Binding Company}" Header="Company" />
</GridView>
</ui:ListView.View>
</ui:ListView>
</controls:ControlExample>
</StackPanel>
</Page>
8 changes: 4 additions & 4 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">
<ListView
<controls:ListView
x:Name="PART_SuggestionsList"
MaxHeight="{TemplateBinding MaxSuggestionListHeight}"
DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
Expand All @@ -154,15 +154,15 @@
ItemsSource="{TemplateBinding ItemsSource}"
KeyboardNavigation.DirectionalNavigation="Cycle"
SelectionMode="Single">
<ListView.ItemsPanel>
<controls:ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel
IsItemsHost="True"
IsVirtualizing="True"
VirtualizationMode="Recycling" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</controls:ListView.ItemsPanel>
</controls:ListView>
</Border>
</Popup>
</Grid>
Expand Down
88 changes: 88 additions & 0 deletions src/Wpf.Ui/Controls/ListView/ListView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace Wpf.Ui.Controls;

/// <summary>
/// Extends <see cref="System.Windows.Controls.ListView"/>, and adds customized support <see cref="ListViewViewState.GridView"/> or <see cref="ListViewViewState.Default"/>.
/// </summary>
/// <example>
/// <code lang="xml">
/// &lt;ui:ListView ItemsSource="{Binding ...}" &gt;
/// &lt;ui:ListView.View&gt;
/// &lt;GridView&gt;
/// &lt;GridViewColumn
/// DisplayMemberBinding="{Binding FirstName}"
/// Header="First Name" /&gt;
/// &lt;GridViewColumn
/// DisplayMemberBinding="{Binding LastName}"
/// Header="Last Name" /&gt;
/// &lt;/GridView&gt;
/// &lt;/ui:ListView.View&gt;
/// &lt;/ui:ListView&gt;
/// </code>
/// </example>
public class ListView : System.Windows.Controls.ListView
Comment thread
koal44 marked this conversation as resolved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
/// <summary>Identifies the <see cref="ViewState"/> dependency property.</summary>
public static readonly DependencyProperty ViewStateProperty = DependencyProperty.Register(nameof(ViewState), typeof(ListViewViewState), typeof(ListView), new FrameworkPropertyMetadata(ListViewViewState.Default, OnViewStateChanged));

/// <summary>
/// Gets or sets the view state of the <see cref="ListView"/>, enabling custom logic based on the current view.
/// </summary>
/// <value>The current view state of the <see cref="ListView"/>.</value>
public ListViewViewState ViewState
{
get => (ListViewViewState)GetValue(ViewStateProperty);
set => SetValue(ViewStateProperty, value);
}

private static void OnViewStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not ListView self)
{
return;
}

self.OnViewStateChanged(e);
}

protected virtual void OnViewStateChanged(DependencyPropertyChangedEventArgs e)
{
// Hook for derived classes to react to ViewState property changes
}

public ListView()
{
Loaded += OnLoaded;
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
Loaded -= OnLoaded; // prevent memory leaks

// Setup initial ViewState and hook into View property changes
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)));
}
}
Loading