2016-12-05 183 views
-1

我是WPF應用程序開發的新手。我想根據塊來獲取樓層。我有一個組合框的塊和另一個組合框的地板。當我在一個組合框中選擇任何塊時,另一個組合框應顯示選擇塊的樓層。 這是組合框佈局:基於另一個組合框值選擇一個組合框值

enter image description here

<ComboBox Grid.Row="0" Grid.Column="0" Width="100" 
      Margin="0,0,0,10" Height="35" 
      Loaded="FrameworkElement_OnLoaded" 
      SelectedValuePath ="Id" 
      SelectedValue ="{Binding SelectedBlockId, Mode=TwoWay}" 
      DisplayMemberPath="Name" 
      SelectionChanged="Selector_OnSelectionChanged" ItemsSource="{Binding Blocks}" /> 

<ComboBox Grid.Row="0" Grid.Column="3" Width="100" 
      Margin="0,0,0,10" Height="35" 
      Loaded="FrameworkElement_OnLoaded" 
      SelectedValuePath ="Id" 
      SelectedValue ="{Binding SelectedFloorId, Mode=TwoWay}" 
      DisplayMemberPath="Name" 
      SelectionChanged="Selector_OnSelectionChanged" ItemsSource="{Binding Floors}" /> 
+0

好像你錯過了你的問題的一些代碼....像'Selector_OnSelectionChanged()' –

+0

私人無效Selector_OnSelectionChanged(對象發件人,SelectionChangedEventArgs E) { } – Zeeshan194

回答

-1

請參考下面的博客文章,瞭解如何使用MVVM模式來實現在WPF級聯組合框的例子:https://blog.magnusmontin.net/2013/06/17/cascading-comboboxes-in-wpf-using-mvvm/

你基本上可以用你的Block和Floor類型代替示例代碼中的Countries和Cities類型。

+0

歡迎來到Stack Overflow!這是一個邊界[僅限鏈接的答案](http://meta.stackexchange.com/q/8231/213671)。你應該擴大你的答案,在這裏包含儘可能多的信息,並使用鏈接僅供參考。 –

0

這裏有一個稍微不同的例子。

XAML中...

<!-- language: xaml --> 
<Label Name="FavoriteFoodLbl" Grid.Column="0" Grid.Row="13">Favorite Food</Label> 
<ComboBox Name="FavoriteFoodCombo" Grid.Column="1" Grid.Row="13" ItemsSource="{Binding Foods}" SelectedItem="{Binding FavoriteFood, UpdateSourceTrigger=PropertyChanged}" /> 

<Label Name="FavoriteFlavourLbl" Grid.Column="2" Grid.Row="13">Favorite Flavour</Label> 
<ComboBox Name="FavoriteFlavourCombo" Grid.Column="3" Grid.Row="13" ItemsSource="{Binding Flavours}" /> 

視圖模型/後臺代碼...

public event PropertyChangedEventHandler PropertyChanged; 

    public ObservableCollection<string> Foods { get; set; } = new ObservableCollection<string>() { "Pizza", "Ice Cream", "Soup" }; 

    private string _favoriteFood; 
    public string FavoriteFood 
    { 
     get { return _favoriteFood; } 
     set 
     { 
      _favoriteFood = value; 
      switch (_favoriteFood) 
      { 
       case "Pizza": 
        _flavours = new ObservableCollection<string>(PizzaToppings); 
        break; 
       case "Ice Cream": 
        _flavours = new ObservableCollection<string>(IceCreamFlavours); 
        break; 
       case "Soup": 
        _flavours = new ObservableCollection<string>(SoupFlavours); 
        break; 
       default: 
        _flavours = new ObservableCollection<string>(); 
        break; 
      } 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FavoriteFood")); 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Flavours")); 
     } 
    } 

    public List<string> PizzaToppings { get; set; } = new List<string>() { "Margarita", "Pepperoni", "Meat Feast" }; 
    public List<string> IceCreamFlavours { get; set; } = new List<string>() { "Vanilla", "Strawberry", "Chocolate" }; 
    public List<string> SoupFlavours { get; set; } = new List<string>() { "Tomato", "Leek and Potato", "Chicken" }; 

    private ObservableCollection<string> _flavours = null; 
    public ObservableCollection<string> Flavours 
    { 
     get 
     { 
      return _flavours; 
     } 
     set 
     { 
      _flavours = value; 
      RaisePropertyChanged(); 
     } 
    } 
    public string FavoriteFlavour { get; set; } 

變化了的情況下語句合適的東西。 這有幫助嗎?

+0

我使用MVVM,所以懇請答覆我根據MVVM架構 – Zeeshan194

+0

該代碼應該在您的視圖模型中工作。不是嗎? –

0

您需要做的是建立BlockFloor模型之間的主從關係,並將它們正確綁定到您View(組合框)。

所以假設你FloorBlock有兩個屬性IDDescription,你的模型應該是這樣的:

public class Floor 
{ 
    public int Id { get; set; } 
    public string Description { get; set; } 
} 

public class Block 
{ 
    public int Id { get; set; } 
    public int Description { get; set; } 
    // notice Floors collection inside each block 
    public IList<Floor> Floors { get; set; } 

    public Block() 
    { 
     Floors = new List<Floor>(); 
    } 
} 

您的視圖模型包含兩個ObservableCollection S和一個屬性來存儲當前選定的區塊。注意SelectedBlock屬性設置器:當屬性更新時,Floors集合將被重新創建爲新值。

public const string BlocksPropertyName = "Blocks"; 
private ObservableCollection<Block> _blocks = null; 
public ObservableCollection<Block> Blocks 
{ 
    get 
    { 
     return _blocks; 
    } 
    set 
    { 
     _blocks = value; 
     RaisePropertyChanged(BlocksPropertyName); 
    } 
} 

public const string SelectedBlockPropertyName = "SelectedBlock"; 
private Block _selectedBlock = null; 
public Block SelectedBlock 
{ 
    get 
    { 
     return _selectedBlock; 
    } 
    set 
    { 
     _selectedBlock = value; 
     RaisePropertyChanged(SelectedBlockPropertyName); 

     if (_selectedBlock != null) 
     { 
      Floors = new ObservableCollection<Floor>(_selectedBlock.Floors); 
     } 
    } 
} 

public const string FloorsPropertyName = "Floors"; 
private ObservableCollection<Floor> _floors = null; 
public ObservableCollection<Floor> Floors 
{ 
    get 
    { 
     return _floors; 
    } 
    set 
    { 
     _floors = value; 
     RaisePropertyChanged(FloorsPropertyName); 
    } 
} 

在XAML中你只是兩個組合框綁定到corrispective集合:

<ComboBox ItemsSource="{Binding Blocks}" 
      SelectedItem="{Binding SelectedBlock}" /> 

<ComboBox ItemsSource="{Binding Floors}" /> 
相關問題