Home > Mobile >  Only show elements of certain types in combobox c#
Only show elements of certain types in combobox c#

Time:05-26

I have a WPF app and i have multiple comboboxes where I'd like each of them to take the same List and then have each one of them take a certain type from the list.

Example of what I want :

enter image description here

enter image description here

What I have :

enter image description here

Current XAML :

            <ComboBox Grid.Column="1" Grid.Row="2" ItemsSource="{Binding Composants}"/> 
            <ComboBox Grid.Column="1" Grid.Row="4"/>
            <ComboBox Grid.Column="1" Grid.Row="6"/>
            <ComboBox Grid.Column="3" Grid.Row="2"/>
            <ComboBox Grid.Column="3" Grid.Row="4"/>
            <ComboBox Grid.Column="3" Grid.Row="6"/>
            <TextBlock  Grid.Column="1" Grid.Row="1" Text="Moteur :" TextWrapping="Wrap" VerticalAlignment="Bottom"/>
            <TextBlock  Grid.Column="1" Grid.Row="3" Text="Suspensions :" TextWrapping="Wrap" VerticalAlignment="Bottom"/>
            <TextBlock  Grid.Column="1" Grid.Row="5" Text="Freins :" TextWrapping="Wrap" VerticalAlignment="Bottom"/>
            <TextBlock  Grid.Column="3" Grid.Row="1" Text="Pneus :" TextWrapping="Wrap" VerticalAlignment="Bottom"/>
            <TextBlock  Grid.Column="3" Grid.Row="3" Text="Transmission :" TextWrapping="Wrap" VerticalAlignment="Bottom"/>
            <TextBlock  Grid.Column="3" Grid.Row="5" Text="Turbo :" TextWrapping="Wrap" VerticalAlignment="Bottom"/>

c#, List loaded from Stub :

        List<Composant> composants = new List<Composant>()
    {
                new Moteur("Moteur de base", 0, 1, 1),
                new Moteur("Moteur de course", 50000, 1.6, 1.2),
                new Moteur("Moteur de compétition", 100000, 1.9, 1.76),

                new Turbo("Turbo de base" , 0, 1, 1),
                new Turbo("Turbo de course", 50000, 1.6, 1.2),
                new Turbo("Turbo de compétition" , 100000, 1.54, 1.76),

                new Suspension("Suspensions de base", 0, 1),
                new Suspension("Suspensions de course", 50000, 1.6),
                new Suspension("Suspensions de compétition", 100000, 1.85),

                new Frein("Freins de base", 0, 1),
                new Frein("Freins de course", 50000, 1.6),
                new Frein("Freins de compétition",  100000, 1.85),

                new Pneus("Pneus de base", 0, 1,1,1),
                new Pneus("Pneus de course", 50000, 1.6,1.5,1.1),
                new Pneus("Pneus de compétition", 100000, 1.85,1.55,1.2)
    };

CodePudding user response:

I think you can populate the lists by checking the type of composant (given that Moteur, Turbo, ... all are derived classes of Composant).

foreach (var item in composants)
{
    if (item is Moteur)
    {
        moteurComboBox.Items.Add(item);
    }
    else if (item is Turbo)
    {
        turboComboBox.Items.Add(item);
    }
    else ...
}
  • Related