使用int数组更新列表框

本文关键字:列表 更新 数组 int 使用 | 更新日期: 2023-09-27 17:50:30

我有一个像这样的BindingList:

private BindingList<int[]> sortedNumbers = new BindingList<int[]>();

每个条目都是int[6],现在我想将它绑定到一个列表框,这样每次添加一组数字时它都会更新。

listBox1.DataSource = sortedNumbers;

结果为每个条目的以下文本:

Matriz Int32[].

如何格式化输出或更改输出,以便在生成每个条目集时打印它们的编号?

使用int数组更新列表框

您需要处理Format事件:

listBox1.Format += (o,e) => 
 { 
    var array = ((int[])e.ListItem).Select(i=>i.ToString()).ToArray();
    e.Value = string.Join(",", array);
 };

如何在ItemTemplate中使用IValueConverter ?

<ListBox x:Name="List1" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Converter={StaticResource  NumberConverter}}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
public class NumberConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is int[])
        {
            int[] intValues = (int[])value;
            return String.Join(",", intValues);
        }
        else return Binding.DoNothing;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Convert(value, targetType, parameter, culture);
    }
}