0

我在我的項目中使用了WPF C#。 我的UI包含4個組合框,每個組合框都將由數據庫中的值加載。 現在,所有4個組合框都會一次顯示。 但我想要做的是,第一個組合框1應該是可見的/顯示,然後用戶選擇一個值,如val1,然後第二個組合框應該是可見的,它應該包含基於combobox1中選擇的val1的值(來自數據庫)等等。 組合框應該與先前的組合框相關聯。WPF中的相關組合框

我該如何在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> 
+0

到目前爲止你的努力是什麼?你遇到什麼問題? –

+0

我可以在combobox1中加載數據,用戶可以選擇一個值,但現在我執行一個查詢,它基於在combobox中選擇的值來過濾數據。我想要將它加載到combobox2中.and combobox 2應該僅在選擇combobox中的值後才顯示1 – beginner

+0

請給我們看一些代碼,刪除不相關的數據庫的東西。 –

回答

0

您可以使用BooleanToVisibilityConverter Class的顯示和隱藏在各種ComboBox ES與數字0結合代碼後面或查看模型的屬性。您可以從相關的SelectionChanged事件處理程序中將相關的設置爲true。就拿這個簡單的例子:

XAML:

<ComboBox Name="Re" Grid.Row="2" Grid.Column="1" VerticalAlignment="Top" 
    Loaded="ComboBox_RCLoaded" SelectionChanged="ComboBox_RCSelectionChanged" 
    Visibility="{StaticResource BooleanToVisibilityConverter}" /> 

代碼背後:

private void ComboBox_PLSelectionChanged(object sender, SelectionChangedEventArgs args) 
{ 
    ComboBox comboBox = sender as ComboBox; 
    var data = GetDataForNextComboBox(comboBox.SelectedItem); 
    NextComboBox.ItemsSource = data; 
    // Make next ComboBox visible using BooleanToVisibilityConverter 
    BoolPropertyForComboBox2 = true; 
} 

很顯然,你仍然需要在你的Resources部分添加到BooleanToVisibilityConverter參考,聲明新的bool屬性並實施GetDataForNextComboBox數據訪問方法。

+0

同意,雖然我們在談論代碼隱藏,但我會直接設置「NextComboBox.Visibility」。如果談論MVVM,我會刪除SelectionChanged事件處理並創建一個綁定到另一個屬性。 – almulo

+0

我在組合框2中嘗試了Visibility =「Hidden」屬性,並試圖在加載combobox1後使其可見。但combo box2應該加載一次,我從combobox1中選擇一個值,因爲它基於該值從數據庫中獲取數據。但現在我正在觀察的是combobox2的Loaded =「ComboBox_RCLoaded」正試圖加載它之前,我選擇一個值combobox1.Can我使Load屬性的組合框等待它,我從combobox1選擇一個值? – beginner

+0

@beginner ComboBox將被加載,但您可以根據Selectionchanged事件中comboBox1的選擇來設置可見性。有一種方法來填充ComboBox2的項目,並在ComboBox1的Selectionchanged事件中調用該方法。否則請讓我們知道您想要在ComboBox2的Loaded事件中精確執行的操作。 – Karuppasamy

0

如果你沒有使用MVVM,然後通過謝里登給出的答案會HEPL你,那不是有一個布爾變量,BoolToVisibility轉換器,你可以設置在事件本身的可見性(如almulo說過)。

如果你或任何人使用MVVM和有這個問題,可以看看我的答案。

我剛剛添加了代碼,以根據ComboBox1的SelectedItem設置可見性,並根據ComboBox1的SelectedItem設置ItemsSource。

關於ViewModel的旅遊推薦。

  1. 有一個屬性綁定到ComboBox1的SelectedItem。
  2. 有一個需要根據ComboBox1的SelectedItem綁定到ComboBox2的集合屬性。
  3. 每當SelectedItemCombo1是變化,更新勢必ComboBox2的的ItemsSource(你可以在COMBO1的的SelectedItem的PropertyChanged事件做到這一點,否則你可以寫在VM爲的SelectionChanged命令)

集合屬性找到ComboBox2的XAML代碼,我添加了轉換器來檢查ComboBox1的SelectedItem,如果它爲null,ComboBox2將不可見。

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

轉換如下:

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(); 
     } 
    } 

希望這有助於你。