1

I have a BindingList like the follow:

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

Each entry is a int[6], now I wanted to bind it to a listbox so it updates it everytime a set of numbers is added to it.

listBox1.DataSource = sortedNumbers;

The result is the below text for each entry:

Matriz Int32[].

How do I format the output or change it so it prints the numbers of each entry set as they are generated ?

2 Answers 2

1

You need to handle the Format event:

listBox1.Format += (o,e) => 
 { 
    var array = ((int[])e.ListItem).Select(i=>i.ToString()).ToArray();
    e.Value = string.Join(",", array);
 };
Sign up to request clarification or add additional context in comments.

3 Comments

+1 interesting didnt know I had a format in there but the cast doesnt seem to work on the join.
Are you using .NET 4.0 or are you using an earlier version than that?
Pardon me ... I am tied to 3.5
0

How about using IValueConverter in the ItemTemplate?

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

1 Comment

I didn't notice that you are using winform. But at least I find another observable collection in your code: "BindingList<T>"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.