locked
How to create a clear combobox selection button RRS feed

  • Question

  • So I'm not even into the coding side of SketchFlow but working only with the visual side.  I have created a group of comboboxes.  I guess I have two questions. 

    1.  Can I make the comboboxes dynamic?   Meaning if I select "Ford" from the first combo box, the next combo box will display models associated with "Ford", so in essence the second combo box will change according to the input of the first combo box?

    2.  Can I make a button to clear or reset the comboboxes to the default index which in my case is 0?

    Friday, November 30, 2012 7:54 PM

All replies

  • Hello Smockpuv.

    Here is a quick sample of using one comboBox to filter another and a couple of "Clear Filter" buttons...

    <Window
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	x:Class="FilterComboBox.MainWindow"
    	x:Name="Window"
    	Title="MainWindow"
    	Width="640" Height="480">
    
    	<Grid x:Name="LayoutRoot">
            <ComboBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox1_SelectionChanged" />
            <ComboBox Height="23" HorizontalAlignment="Left" Margin="154,10,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" />
            <Button x:Name="clearFilterButton" Content="Clear Filter" VerticalAlignment="Top" Margin="304,8,245,0" Click="clearFilterButton_Click"/>
            <Button x:Name="clearFilterButton2" Content="Clear Filter 2" VerticalAlignment="Top" Margin="304,33.96,245,0" Click="clearFilterButton_Click2"/>
        </Grid>
    </Window>


    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    
    namespace FilterComboBox
    {
    	public partial class MainWindow : Window
    	{
            	ObservableCollection<Cars> CarCollection = new ObservableCollection<Cars>() 
            	{ 
                		new Cars { Make = "Acura", Model = "MDX"},
                		new Cars { Make = "Ford", Model = "Explorer" },
    		        new Cars { Make = "Ford", Model = "Escape" },
                		new Cars { Make = "GMC", Model = "Sierra" }
            	};
            
            	CollectionView View;
    
    		public MainWindow()
    		{
    			this.InitializeComponent();
                		View = (CollectionView)CollectionViewSource.GetDefaultView(CarCollection);
                
                		comboBox1.Items.Add("All");
    			comboBox1.Items.Add("Acura");
                		comboBox1.Items.Add("Ford");
                		comboBox1.Items.Add("GMC");
                		comboBox1.SelectedIndex = 0;
    
    	            	//The following will filter the second ComboBox based on the item selected in the first ComboBox.
    		        View.Filter = null;
    		        comboBox2.ItemsSource = View;
            	    	comboBox2.DisplayMemberPath = "Model";
    			comboBox2.IsDropDownOpen = true;
    		}
    
    	        public bool Contains(object car)
            	{
            	    Cars cars = car as Cars;
    	            return (cars.Make == comboBox1.SelectedItem.ToString());
            	}
    
    	        private void comboBox1_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
            	{
    	            	//Refreshes comboBox2 when a new Make is selected in comboBox1.
    			//Setting the View.Filter to null will remove the filter and show all items.
    			
    			if (comboBox1.SelectedItem.ToString() == "All")
    				View.Filter = null;
    			else
    				View.Filter = new Predicate<object>(Contains);
    			
    			comboBox2.Focus();
    			comboBox2.IsDropDownOpen = true;
            	}
    
    	        private void clearFilterButton_Click(object sender, System.Windows.RoutedEventArgs e)
            	{
    			//This does not change the first comboBox selection.
            		View.Filter = null;
    			comboBox2.Focus();
    			comboBox2.IsDropDownOpen = true;
            	}
    
    	        private void clearFilterButton_Click2(object sender, System.Windows.RoutedEventArgs e)
            	{
    			//By changing the first comboBox selection the filter is automatically applied.
    			//For index 0 or "All" the filter would be set to null.
            		comboBox1.SelectedIndex = 0;
            	}
    	}
    	
        public class Cars
        {
            #region
    
            private string make;
            public string Make { get { return make; } set { make = value; OnPropertyChanged("Make"); } }
    
            private string model;
            public string Model { get { return model; } set { model = value; OnPropertyChanged("Model"); } }
    
            #endregion
    
            #region INotifyPropertyChanged
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
    
            #endregion
        }
    }

    I have uploaded the sample to my SkyDrive if you'd like to test it out...  http://sdrv.ms/VimNUd

    You may want to take a look at this as well... CollectionView.Filter Property.

    I hope that gives you some ideas.

    ~Christine


    My Gallery - calControls


    Sunday, December 2, 2012 2:05 AM