Create a collection with various data types and bind to a list

I am trying to create a collection with various data types to bind to a list control in Silverlight and C #.

Is this possible if each of the data types implements an interface?

For example, I have separate objects “Violin”, “Guitar” and “Drums”, each of which implements the “IMusicalInstrument” interface. Can I create a list, associate it with the list, and also add the objects “Violin”, “Guitar” and “Drums” to this list?

+3
source share
2 answers

Yes, you can, using the Generic list , take a look List<T>. You can create a list using tools like this:

var instruments = new List<IMusicalInstrument> { 
    new Drum(),
    new Guitar(),
    new Violin()
}

and then use instrumentsListBox.DataSource = instruments;

However , if you want to make it easier for yourself, tell them to implement the properties for DisplayMemberand ValueMember, this is what they use ListBoxto determine what to show and use as value when choosing something.

+5
source

, Silverlight , . , , , , , , . , datatemplate , , , . , , , .. , , :

public class Drum
{
    public int DrummerCount { get; set; }
}

public class Violin
{
    public int ViolinistCount { get; set; }
}

public class Guitar
{
    public int GuitaristCount { get; set; }
}

ListBox, :

    <ListBox x:Name="instrumentsListBox">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock 
                        Visibility="{Binding Converter={StaticResource instrumentVisibilityConverter}, ConverterParameter=Drum}" 
                        Text="{Binding DrummerCount, StringFormat=Drummers:\{0\}, Converter={StaticResource debugConverter}}"/>
                    <TextBlock 
                        Visibility="{Binding Converter={StaticResource instrumentVisibilityConverter}, ConverterParameter=Violin}" 
                        Text="{Binding ViolinistCount, StringFormat=Violinists:\{0\}, Converter={StaticResource debugConverter}}"/>
                    <TextBlock 
                        Visibility="{Binding Converter={StaticResource instrumentVisibilityConverter}, ConverterParameter=Guitar}" 
                        Text="{Binding GuitaristCount, StringFormat=Guitarists:\{0\}, Converter={StaticResource debugConverter}}"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

, TextBlock, . TextBlock InstrumentVisibilityConverter, :

public class InstrumentVisibilityConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string expectedType = parameter as string;
        if (value.GetType().Name == expectedType)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

, (, ):

List<object> instruments = new List<object> 
    { 
        new Guitar(), 
        new Guitar(), 
        new Violin(), 
        new Violin(), 
        new Drum() 
    };
instrumentsListBox.ItemsSource = instruments;

(. ), Silverlight . , , . , , .

0

Source: https://habr.com/ru/post/1786790/


All Articles