2011-04-18 41 views
1

我有一個類似的BindingList後續:BindingList與int數組更新列表框?

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

每一項都是一個INT [6],現在我想所以每次更新它的一組數字被添加到其將其綁定到一個列表框。

listBox1.DataSource = sortedNumbers; 

結果是爲每個條目下面的文字:

Matriz Int32[]. 

我如何格式化輸出或改變它,所以它打印每個條目的數字作爲生成它們設置?

回答

1

您需要處理Format事件:

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

+1有趣的不知道我有一個格式在那裏,但演員似乎並沒有工作的聯接。 – Prix 2011-04-18 04:51:35

+0

您使用的是.NET 4.0還是使用的是早期版本? – 2011-04-18 04:53:44

+0

對不起我......我被綁在3.5 – Prix 2011-04-18 04:54:32

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); 
    } 
} 
+0

我沒有注意到你正在使用winform。但至少我在代碼中找到另一個可觀察的集合:「BindingList 」 – Howard 2011-04-18 05:38:40