WPF-级联组合框无法正常工作

时间:2015-11-05 04:57:23

标签: c# wpf combobox cascading

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

根据在第一个组合框中选择的服务器名称填充第二个组合框的功能

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

我无法在第一个组合框中选择值的基础上填充第二个组合框。我能正确绑定两个组合框吗?

1 个答案:

答案 0 :(得分:0)

您已将第二个comboBox绑定到名为 DBNameList

的属性

你还没有写任何 RaisePropertyChanged

所以你的财产就像这样

public List<string> DBNameList
        {
            get { return _DBNameList; }
            set
            {
                if(_DBNameList != value)
                {
                    _DBNameList = value;
                    RaisePropertyChanged("DBNameList");
                }
            }
        }