admin管理员组

文章数量:1201975

In my WPF project, I have a ListView's data binding set to an ObservableCollection of my model DashboardSample as follows:

public ObservableCollection<DashboardSample> SampleOutput { get; set; } = new() { };

One of the fields in DashboardSample is a list of doubles:

public List<double> EffluxTimeList { get; set; } = new();

However, this field is displaying as only the first double in the list, surrounded by square brackets, in the ListView.

Is there a solution to displaying the full list without manually turning it into a formatted string?

I ensured my data binding was working for other fields, and that the binding variable was properly assigned.

In my WPF project, I have a ListView's data binding set to an ObservableCollection of my model DashboardSample as follows:

public ObservableCollection<DashboardSample> SampleOutput { get; set; } = new() { };

One of the fields in DashboardSample is a list of doubles:

public List<double> EffluxTimeList { get; set; } = new();

However, this field is displaying as only the first double in the list, surrounded by square brackets, in the ListView.

Is there a solution to displaying the full list without manually turning it into a formatted string?

I ensured my data binding was working for other fields, and that the binding variable was properly assigned.

Share Improve this question edited Jan 23 at 3:29 Rafael 2,0092 gold badges25 silver badges62 bronze badges asked Jan 21 at 15:46 NoahNoah 91 bronze badge 4
  • How does the binding look like now? – Klaus Gütter Commented Jan 21 at 15:50
  • A nested ItemsControl in the ItemTemplate of the ListBox would work. Or a Binding Converter that creates an appropriate string representation. Please clarify how exactly it should look like, and show us the relevant parts of your current XAML. – Clemens Commented Jan 21 at 15:53
  • I would like it to appear as the list, without square brackets, and separated by commas (i.e. 100, 200, 300). My ListView XAML is <ListView ItemsSource="{Binding SampleOutput}" Foreground="{StaticResource TextBrush}" View="{Binding ColumnConfig, Converter={StaticResource ConfigToDynamicGridViewConverter}}"/> – Noah Commented Jan 21 at 16:06
  • 1 Please do not post code in comments, but edit your question. Relevant XAML should also show the View, i.e. the result of {Binding ColumnConfig, Converter={StaticResource ConfigToDynamicGridViewConverter}}. But since you know how converters work, why don't you simply write one for the EffluxTimeList Binding? – Clemens Commented Jan 21 at 16:12
Add a comment  | 

1 Answer 1

Reset to default 0

You can create a value converter that formats the list into a string representation suitable for display. This way, you don't need to modify your model class or convert the list every time you update it.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Data;

public class DoubleListConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var list = value as List<double>;
        return list != null ? String.Join(", ", list) : "";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException(); // One-way binding
    }
}

Add the converter to your XAML resources:

<Window.Resources>
    <local:DoubleListConverter x:Key="DoubleListConverter"/>
</Window.Resources>

Use the converter in your ListView's column binding:

<ListView ItemsSource="{Binding SampleOutput}">
    <ListView.View>
        <GridView>
            <!-- Other columns -->
            <GridViewColumn Header="Efflux Time List">
                <GridViewColumn.DisplayMemberBinding>
                    <Binding Path="EffluxTimeList" Converter="{StaticResource DoubleListConverter}" />
                </GridViewColumn.DisplayMemberBinding>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

本文标签: cObservableCollection of Doubles in WPF is Showing Only First ItemStack Overflow