我有一個 WPF 應用程式,并且我有多個組合框,我希望它們中的每一個都采用相同的串列,然后讓它們中的每一個從串列中采用某種型別。
我想要的例子:


我有的 :

當前 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<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)
};
uj5u.com熱心網友回復:
我認為您可以通過檢查的型別來填充串列composant(假設Moteur, Turbo, ... 都是 的派生類Composant)。
foreach (var item in composants)
{
if (item is Moteur)
{
moteurComboBox.Items.Add(item);
}
else if (item is Turbo)
{
turboComboBox.Items.Add(item);
}
else ...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/481271.html
