Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. For example combobox1 has 2 listboxitems:
  2. Fruit
  3. Cars
  4.  
  5. If Fruit is selected on first combobox, the second combobox will display listboxes:
  6.  
  7. Apple
  8. Orange
  9.  
  10. If Cars is selected on first combobox, the second combobox will display listboxes:
  11.  
  12. Honda
  13. Nissan
  14.  
  15. <ComboBox Name="comboBox1" Canvas.Left="74" Canvas.Top="527" Width="120">
  16. <ListBoxItem Content="Fruit"/>
  17. <ListBoxItem Content="Car"/>
  18. </ComboBox>
  19.  
  20. <ComboBox Name="comboBox2" Canvas.Left="74" Canvas.Top="527" Width="120">
  21. <ListBoxItem Content="Apple"/>
  22. <ListBoxItem Content="Orange"/>
  23.  
  24. <ListBoxItem Content="Honda"/>
  25. <ListBoxItem Content="Nissan"/>
  26. </ComboBox>
  27.  
  28. <Window x:Class="WpfApplication1.MainWindow"
  29. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  30. xmlns:converters="clr-namespace:WpfApplication1"
  31. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  32. Title="MainWindow" Height="350" Width="525">
  33. <StackPanel>
  34. <ComboBox Name="comboBox1" Width="120" Margin="0,25,0,50" ItemsSource="{Binding Types}" SelectedItem="{Binding SelectedType}"/>
  35. <ComboBox Name="comboBox2" Width="120" ItemsSource="{Binding Items}">
  36. <ComboBox.ItemTemplate>
  37. <DataTemplate>
  38. <TextBlock Text="{Binding Value}"/>
  39. </DataTemplate>
  40. </ComboBox.ItemTemplate>
  41. </ComboBox>
  42. </StackPanel>
  43. </Window>
  44.  
  45. /// <summary>
  46. /// Interaction logic for MainWindow.xaml
  47. /// </summary>
  48. public partial class MainWindow : Window
  49. {
  50. MainViewModel vm = new MainViewModel();
  51. public MainWindow()
  52. {
  53. InitializeComponent();
  54. this.DataContext = vm;
  55. }
  56. }
  57.  
  58. namespace WpfApplication1
  59. {
  60. public class MainViewModel : INotifyPropertyChanged
  61. {
  62. #region Members
  63. private ItemType _selectedType;
  64. private List<Item> _allItems;
  65. private List<Item> _items;
  66. #endregion Members
  67.  
  68. #region Properties
  69. /// <summary>
  70. /// Gets or sets the Selected Item Type
  71. /// </summary>
  72. /// <value>
  73. /// The selected Item Type
  74. /// </value>
  75. public ItemType SelectedType
  76. {
  77. get { return _selectedType; }
  78. set
  79. {
  80. _selectedType = value;
  81. Filter(); // Filter items list once SelectedType has changed
  82. }
  83. }
  84.  
  85. /// <summary>
  86. /// Gets a list of all Item Types
  87. /// </summary>
  88. public List<ItemType> Types { get; private set; }
  89.  
  90. /// <summary>
  91. /// Gets or sets the currently filltered list of Items
  92. /// </summary>
  93. /// <value>
  94. /// The fitlered items.
  95. /// </value>
  96. public List<Item> Items
  97. {
  98. get { return _items; }
  99. set
  100. {
  101. _items = value;
  102. NotifyPropertyChanged("Items");
  103. }
  104. }
  105. #endregion Properties
  106.  
  107. public MainViewModel()
  108. {
  109. Types = new List<ItemType>
  110. {
  111. ItemType.All,
  112. ItemType.Fruit,
  113. ItemType.Car
  114. };
  115.  
  116. _allItems = new List<Item>
  117. {
  118. new Item
  119. {
  120. Value = "Apple",
  121. Type = ItemType.Fruit
  122. },
  123. new Item
  124. {
  125. Value = "Orange",
  126. Type = ItemType.Fruit
  127. },
  128. new Item
  129. {
  130. Value = "Honda",
  131. Type = ItemType.Car
  132. },
  133. new Item
  134. {
  135. Value = "Nissan",
  136. Type = ItemType.Car
  137. }
  138. };
  139. Items = _allItems;
  140. }
  141.  
  142. /// <summary>
  143. /// Filters the Items list based on currently selected Item Type
  144. /// </summary>
  145. private void Filter()
  146. {
  147. var filteredItems = new List<Item>();
  148. if (ItemType.All == _selectedType)
  149. {
  150. filteredItems = _allItems;
  151. }
  152. else
  153. {
  154. foreach (var item in _allItems)
  155. {
  156. if (item.Type == _selectedType)
  157. {
  158. filteredItems.Add(item);
  159. }
  160. }
  161. }
  162. Items = filteredItems;
  163. }
  164.  
  165. #region INotifyPropertyChanged
  166. public event PropertyChangedEventHandler PropertyChanged;
  167. private void NotifyPropertyChanged(String propertyName)
  168. {
  169. if (PropertyChanged != null)
  170. {
  171. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  172. }
  173. }
  174. #endregion
  175. }
  176.  
  177. public enum ItemType
  178. {
  179. All,
  180. Fruit,
  181. Car
  182. }
  183.  
  184. public class Item
  185. {
  186. public string Value { get; set; }
  187. public ItemType Type { get; set; }
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement