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 |1 Answer
Reset to default 0You 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
版权声明:本文标题:c# - ObservableCollection of Doubles in WPF is Showing Only First Item - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738620477a2103160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<ListView ItemsSource="{Binding SampleOutput}" Foreground="{StaticResource TextBrush}" View="{Binding ColumnConfig, Converter={StaticResource ConfigToDynamicGridViewConverter}}"/>
– Noah Commented Jan 21 at 16:06{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