Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XAML
XML
<ComboBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Center" Margin="2,2,0,0" Name="comboBoxServer" VerticalAlignment="Top" Width="156" ItemsSource="{Binding ServerNameList}"   SelectionChanged="comboBoxServer_SelectionChanged" SelectedValuePath="key" SelectedValue="{Binding serverSelected, Mode=OneWayToSource,UpdateSourceTrigger=PropertyChanged}" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding serverCommand}" CommandParameter="{Binding  ElementName=comboBoxServer,Path=SelectedItems}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

<ComboBox Grid.Column="1" Grid.Row="2" Height="23" HorizontalAlignment="Center" Margin="2,2,0,0" Name="comboBoxDBName" VerticalAlignment="Top" Width="156" ItemsSource="{Binding Path=DBNameList}" SelectionChanged="comboBoxServer_SelectionChanged" SelectedValuePath="Key" SelectedValue="{Binding serverSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
</ComboBox>

DataContext.cs
C#
public List<string> GetServer(string server)
{
    var keys = ConfigurationManager.AppSettings.Keys;
    return keys.Cast<object>()
               .Where(key => key.ToString().ToLower()
               .Contains(server.ToLower()))
               .Select(key => ConfigurationManager.AppSettings.Get(key.ToString())).ToList();
}

C#
public List<string> GetDBNames(string sqlServerName)
{
    List<string> lstDBName = new List<string>();

    sqlServerName = openconn().DataSource;
    SqlConnectionStringBuilder connection = new SqlConnectionStringBuilder();
    connection.DataSource = sqlServerName;

    connection.IntegratedSecurity = true;
    String strCon = connection.ToString();
    SqlConnection sqlConn = new SqlConnection(strCon);
    sqlConn.Open();

    SqlCommand cmd = new SqlCommand("select * from sys.databases", sqlConn);
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        lstDBName.Add(dr[0].ToString());
    }
    sqlConn.Close();
    return lstDBName;
}

BackupViewModel.cs
C#
List<string> _ServerNameList;
List<string> _DBNameList;               

public List<string> ServerNameList
{
    get { return _ServerNameList; }
    set
    {
        _ServerNameList = value;                
    }
}

public List<string> DBNameList
{
    get { return _DBNameList; }
    set
    {
        _DBNameList = value;
    }
}

string _serverSelected;
public string serverSelected
{
    get { return _serverSelected; }
    set
    {
        if ( _serverSelected!=value)                    
            _serverSelected = value;

        RaisePropertyChanged("serverSelected");              
    }
}

public BackUpViewModel()
{    
     BackUpContext servObj = new BackUpContext();
    _ServerNameList = servObj.GetServer("ServerName");
    _DBNameList = servObj.GetDBNames(serverSelected);                
    serverCommand = new RelayCommand(fetchServer);
}
public RelayCommand serverCommand { get; set; }
public void fetchServer(object server)
{
     serverSelected = server.ToString();               
}

serverCommand = new RelayCommand(fetchServer);
Posted
Updated 4-Nov-15 21:12pm
v2
Comments
VR Karthikeyan 5-Nov-15 0:32am    
What is your question? Explain your problem.
Member 12002095 5-Nov-15 0:34am    
I am not able to populate second combo box filtered on the basis of selection of value in first combo box. Am i binding the both combo boxes correctly?

If i select any value in first combo box, the second combo box always remains populated with same database name value. I want that based on selection in first combo box, second combo box should be filtered and then populate.
Gokulprasad05 5-Nov-15 3:32am    
Refer the link :
http://blog.magnusmontin.net/2013/06/17/cascading-comboboxes-in-wpf-using-mvvm/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900