WPF-级联组合框不起作用

本文关键字:不起作用 组合 级联 WPF- | 更新日期: 2023-09-27 18:20:40

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>

数据上下文.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);

我无法填充根据第一个组合框中的值选择过滤的第二个组合框。我是否正确绑定了两个组合框?

WPF-级联组合框不起作用

您已经将第二个comboBox绑定到名为DBNameList的属性

您还没有为该编写任何RaisePropertyChanged

所以你的房产是这样的

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