1

我有一个如下所示的 BindingList:

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

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

listBox1.DataSource = sortedNumbers;

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

Matriz Int32[].

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

4

2 回答 2

1

您需要处理Format事件:

listBox1.Format += (o,e) => 
 { 
    var array = ((int[])e.ListItem).Select(i=>i.ToString()).ToArray();
    e.Value = string.Join(",", array);
 };
于 2011-04-18T04:47:15.820 回答
0

在 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);
    }
}
于 2011-04-18T04:49:33.803 回答