8

wpf に 2 つのコンボ ボックスがあり、コンボ ボックスの 1 つは次のようになります。

            <ComboBox Height="23" HorizontalAlignment="Left" Margin="244,10,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120">
                <ComboBoxItem Content="Peugeut" />
                <ComboBoxItem Content="Ford" />
                <ComboBoxItem Content="BMW" />
            </ComboBox>

2 番目のコンボボックス 2 をどのようにバインドして、特定の車種をコンボボックス 1 で選択した項目にリストするのか疑問に思っていました。

Peurgeut が選択されている場合、コンボボックス 2 にリストが表示されます。

106
206
306 

またはbmwが選択されている場合

4 series
5 series

等々

4

3 に答える 3

9
    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>

    <ComboBox Height="23" ItemsSource="{Binding Cars}" DisplayMemberPath="Name" HorizontalAlignment="Left" Margin="244,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120"/>
    <ComboBox Height="23" Grid.Row="1" ItemsSource="{Binding SelectedItem.Series, ElementName=comboBox1}" HorizontalAlignment="Left" Margin="244,10,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120"/>

</Grid>

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Cars = new ObservableCollection<Car>();
        Cars.Add(new Car() { Name = "Peugeut", Series = new ObservableCollection<string>() { "106", "206", "306" } });
        Cars.Add(new Car() { Name = "Ford", Series = new ObservableCollection<string>() { "406", "506", "606" } });
        Cars.Add(new Car() { Name = "BMW", Series = new ObservableCollection<string>() { "706", "806", "906" } });
        DataContext = this;

    }

    public ObservableCollection<Car> Cars { get; set; }

}
public class Car
{
    public string Name { get; set; }
    public ObservableCollection<string> Series { get; set; }
}

これが役立つことを願っています。

于 2012-07-26T03:44:41.320 に答える
1

データを調べない限り、XAML だけではできないと思います。ただし、コンボ ボックスをバインドするクラスを作成した場合は、次のようなクラスを持つことができます。

public class CarMake
{
    public string Make {get; set;}
    public List<string> Models {get; set;}
}

次に、最初のコンボ ボックスで、情報が入力された List のインスタンスにバインドし、次のように 2 番目のコンボ ボックスをバインドします。

<ComboBox ItemsSource="{Binding ElementName=FirstComboBox, Path=SelectedItem.Models}" ></ComboBox>

それはあなたを動かすはずです...

于 2012-07-26T03:36:56.550 に答える
0

ユーザーが ComboBox1 アイテムを選択したときに、プログラムで box2 にアイテムを追加してみてください。

        if (combobox1.SelectedText == "Peurgeut")
        {
            box2.Items.Add("106");
            box2.Items.Add("206");
            box2.Items.Add("306");
        }
于 2012-07-26T03:33:37.350 に答える