0

プロジェクトで WPF C# を使用しています。私のUIには4つのコンボボックスが含まれており、それぞれがデータベースからの値によってロードされます。一度に 4 つのコンボボックスがすべて表示されるようになりました。しかし、私がやりたいことは、最初のコンボボックス1が表示/表示され、次にユーザーがval1という値を選択し、次に2番目のコンボボックスが表示され、コンボボックス1で選択されたval1に基づいて(データベースから)値が含まれている必要があります。すぐ。コンボ ボックスは、前のコンボボックスと相互に関連している必要があります。

WPFでこのタスクを達成するにはどうすればよいですか?

XAML コードの一部: 選択してください:

    <Label Grid.Row="1">combobox1 :</Label>
    <ComboBox  Name="PL" Grid.Row="1" Grid.Column="1" Loaded="ComboBox_PLLoaded" SelectionChanged="ComboBox_PLSelectionChanged" />

    <Label Grid.Row="2" HorizontalAlignment="Right">combobox2:</Label>
    <ComboBox Name="Re" Grid.Row="2" Grid.Column="1" VerticalAlignment="Top"  Loaded="ComboBox_RCLoaded" SelectionChanged="ComboBox_RCSelectionChanged"/>

    <Label Grid.Row="3" Margin="89.216,0,60.581,26" Grid.RowSpan="2">combobox3 :</Label>
    <ComboBox Name="CT" Grid.Row="3" Grid.Column="1" Loaded="ComboBox_RCLoaded" SelectionChanged="ComboBox_RCSelectionChanged"/>

    <Label Grid.Row="4" HorizontalAlignment="Right">combobox4 :</Label>
    <ComboBox Name="PT" Grid.Row="4" Grid.Column="1" />

   </Grid>
4

2 に答える 2

0

If you are not using MVVM, then the answer given by Sheridan would hepl you, in that instead of having a bool variable and BoolToVisibility converter, you can just set the Visibility in that event itself(as almulo said).

And if you or anybody using MVVM and having this problem, can have a look at my answer.

I have just added the code to set the visibility based on the SelectedItem of ComboBox1 and set ItemsSource based on the SelectedItem of ComboBox1.

Things to do in ViewModel.

  1. Have a property which bind to the SelectedItem of ComboBox1.
  2. Have a collection property which need to be bound to the ComboBox2 based on the SelectedItem of ComboBox1.
  3. Whenever the SelectedItemCombo1 is changes, update the collection property bound to the ItemsSource of ComboBox2 (you can do it at the PropertyChanged event of SelectedItem of Combo1, else you can write a command for SelectionChanged in VM)

Find the XAML code for ComboBox2, I have added converter to check for the SelectedItem of ComboBox1, if it is null, the ComboBox2 will not be visible.

<ComboBox Name="Re" Grid.Row="2" Grid.Column="1" VerticalAlignment="Top"  ItemsSoure="{Binding combo1Selected}"
    Visibility="{Binding ElementName=PL, Path=SelectedItem, Converter={StaticResource selectedItemToVisibilityConverter}}"> />

Converter as follows:

public class SelectedItemToVisibility : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return Visibility.Collapsed;
            else
                return Visibility.Visible;
        }

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

Hope this helps you.

于 2015-06-16T14:03:34.450 に答える