Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListView does not display columns #918

Open
afluegge opened this issue Jan 19, 2024 · 3 comments
Open

ListView does not display columns #918

afluegge opened this issue Jan 19, 2024 · 3 comments
Labels
bug Something isn't working ⭐ top bug Top bug.

Comments

@afluegge
Copy link

Describe the bug

When using a ListView with columns defined, the ListView does not display the columns, instead the ListViewItems are displayed using their ToString()

To Reproduce

  1. Create a new "Compact" WPF UI project from project templates.
  2. Change XAML of Dashboard page to:
    <DockPanel>

        <TextBlock DockPanel.Dock="Top" Text="Sample Text" FontSize="20" FontWeight="Bold" FontStyle="Italic"  Margin="0, 0, 0, 15"/>

        <ListView ItemsSource="{Binding ViewModel.ListViewItems}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=FirstName}" />
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=LastName}" />
                </GridView>
            </ListView.View>
        </ListView>

    </DockPanel>
  1. Change the corresponding ViewModel to:
using System.Collections.ObjectModel;
using Bogus;

namespace ListViewTest.ViewModels.Pages;

public partial class DashboardViewModel : ObservableObject
{
    [ObservableProperty]
    private ObservableCollection<Person> _listViewItems;


    public DashboardViewModel()
    {
        var listViewItems = new Faker<Person>()
            .RuleFor(u => u.FirstName, f => f.Name.FirstName())
            .RuleFor(u => u.LastName, f => f.Name.LastName())
            .Generate(150);

        ListViewItems = new ObservableCollection<Person>(listViewItems);
    }
}


public record Person
{
    public string FirstName { get; init; }
    public string LastName { get; init; }

    /// <inheritdoc />
    public override string ToString()
    {
        return $"{FirstName} {LastName}";
    }
}

Expected behavior

The ListView should display the ListViewItems in columns.

Screenshots

No response

OS version

Windows 10 & 11

.NET version

.NET 8

WPF-UI NuGet version

Latest from Main

Additional context

No response

@afluegge afluegge added the bug Something isn't working label Jan 19, 2024
@KillyMXI
Copy link

I have the same issue with 3.0.0-preview.13.
GridView is unavailable, essentially.

@KillyMXI
Copy link

KillyMXI commented Jan 31, 2024

Workaround:
Define ListView.ItemContainerStyle

<ListView ItemsSource="{Binding ViewModel.Items}">
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <GridViewRowPresenter Content="{TemplateBinding Content}" Columns="{TemplateBinding GridView.ColumnCollection}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Key" DisplayMemberBinding="{Binding Key}" />
            <GridViewColumn Header="Value" CellTemplateSelector="{StaticResource MyTemplateSelector}" />
        </GridView>
    </ListView.View>
</ListView>

Reference: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/how-to-style-a-row-in-a-listview-that-implements-a-gridview?view=netframeworkdesktop-4.8

@david-risney
Copy link

I'm looking to move my project to use WPFUI and I too am running into this issue. Looks great otherwise though so really hoping to see resolution. I'll also try this workaround, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ⭐ top bug Top bug.
Projects
None yet
Development

No branches or pull requests

3 participants