繁体   English   中英

具有int数组的BindingList更新列表框?

[英]BindingList with int array updating a listbox?

我有一个如下的BindingList:

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

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

listBox1.DataSource = sortedNumbers;

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

Matriz Int32[].

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

您需要处理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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM