-1

XAML

    <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

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

function to populate second combo box based on server name selected in first combo box

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

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

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?

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Aman Jain
  • 41
  • 2
  • 6
  • What's happening when you run it? What are you expecting to happen? – Xiao Nov 05 '15 at 05:03
  • @Xiao : 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. – Aman Jain Nov 05 '15 at 05:16

1 Answers1

0

You have bind your second comboBox to property called DBNameList

And you have not written any RaisePropertyChanged for that

So your property goes like this

public List<string> DBNameList
        {
            get { return _DBNameList; }
            set
            {
                if(_DBNameList != value)
                {
                    _DBNameList = value;
                    RaisePropertyChanged("DBNameList");
                }
            }
        }
Vivek Saurav
  • 2,235
  • 4
  • 25
  • 46
  • _DBNameList = servObj.GetDBNames(serverSelected); this statement is causing me error, because i have only one server in my system. So when i am selecting any other server I want that database list the second combo box becomes empty. error : In GetDBname() : {"Value cannot be null.\r\nParameter name: Data Source"} – Aman Jain Nov 05 '15 at 10:19
  • Put a breakpoint in your GetDBNames function and see what is null in this function – Vivek Saurav Nov 05 '15 at 12:17
  • I want that serverSelected value should be passed toh GetDBName(). and then set new dataSource to that selected server and build a new connection string and retrieve those database in that server. For now it is always happening that the data source mentioned in connection string is always being passed to getDBNames () as parameter. So every time same databases are being fetched. – Aman Jain Nov 05 '15 at 12:36