簡體   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