本文介绍了如何绑定级联组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 ComboBox1 < ComboBox Grid.Column = 1 Grid.Row = 1 高度 = 23 HorizontalAlignment = 中心 保证金 = 2,2,0,0 名称 = comboBoxServer VerticalAlignment = 顶部 宽度 = 156 ItemsSource = {Binding ServerNameList} SelectedValuePath = key SelectedValue = {Binding serverSelected,Mode = OneWayToSource,UpdateSourceTrigger = PropertyChanged} > < i:Interaction.Triggers > < i:EventTrigger EventName = SelectionChanged > < i:InvokeCommandAction 命令 = {Binding serverCommand} CommandParameter = {Binding ElementName = comboBoxServer,Path = SelectedItem} / > < / i:EventTrigger > < / i:Interaction.Triggers > < / ComboBox > ComboBox2 < ComboBox Grid.Column =1Grid.Row =2Height =23HorizontalAlignment =CenterMargin =2,2,0,0Name =comboBoxDBNameVerticalAlignment =TopWidth =156ItemsSource ={Binding DBNameList}SelectionCh anged =comboBoxServer_SelectionChangedSelectedValuePath =KeySelectedValue ={Binding serverSelected,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}>< / ComboBox> ViewModel List< string> _ServerNameList; List< string> _DBNameList; public List< string> ServerNameList { get {return _ServerNameList; } 设置 { _ServerNameList = value; } } public List< string> DBNameList { get {return _DBNameList; } set { if(_DBNameList!= value) { _DBNameList = value; RaisePropertyChanged(DBNameList); } } } string _serverSelected; public string serverSelected { get {return _serverSelected; } 设置 { if(_serverSelected!= value) { _serverSelected = value; RaisePropertyChanged(serverSelected); } } } public BackUpViewModel() { BackUpContext servObj = new BackUpContext(); _ServerNameList = servObj.GetServers(); serverCommand = new RelayCommand(fetchServer); _DBNameList = servObj.GetDatabases(serverSelected); } public RelayCommand serverCommand {get;组; public void fetchServer(对象服务器) { serverSelected = server.ToString(); } ModelClass public List< string> GetServers() { XmlDocument doc = new XmlDocument(); doc.Load(@C:\ Users \477340 \Desktop\\\ewFile.xml); XmlNodeList节点 = doc.SelectNodes(/ servers / server); foreach(节点中的XmlNode节点) { lstServerName.Add(node.Attributes [name]。InnerText); } 返回lstServerName; } public List< string> GetDatabases(字符串服务器) { XmlDocument doc = new XmlDocument(); doc.Load(@C:\ Users \477340 \Desktop\\\ewFile.xml); XmlNodeList节点 = doc.SelectNodes(/ servers / server); foreach(节点中的XmlNode节点) { if(node.Attributes [name]。InnerText == server) { XmlNodeList databases = node.SelectNodes(database); foreach(数据库中的XmlNode数据库) { lstDBName.Add(db.InnerText); } } } 返回lstDBName; } 解决方案 试试这些链接 - Cascading Dropdown& ListBox中的多选选项使用WPF [ ^ ] http://social.technet.microsoft.com/wiki/contents/articles/17946.cascading-comboboxes-in-wpf-using-mvvm.aspx [ ^ ] ComboBox1<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}" 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=SelectedItem}"/> </i:EventTrigger> </i:Interaction.Triggers> </ComboBox>ComboBox2<ComboBox Grid.Column="1" Grid.Row="2" Height="23" HorizontalAlignment="Center" Margin="2,2,0,0" Name="comboBoxDBName" VerticalAlignment="Top" Width="156" ItemsSource="{Binding DBNameList}" SelectionChanged="comboBoxServer_SelectionChanged" SelectedValuePath="Key" SelectedValue="{Binding serverSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ></ComboBox>ViewModel List<string> _ServerNameList; List<string> _DBNameList; public List<string> ServerNameList { get { return _ServerNameList; } set { _ServerNameList = value; } } public List<string> DBNameList { get { return _DBNameList; } set { if (_DBNameList != value) { _DBNameList = value; RaisePropertyChanged("DBNameList"); } } } string _serverSelected; public string serverSelected { get { return _serverSelected; } set { if (_serverSelected != value) { _serverSelected = value; RaisePropertyChanged("serverSelected"); } } } public BackUpViewModel() { BackUpContext servObj = new BackUpContext(); _ServerNameList = servObj.GetServers(); serverCommand = new RelayCommand(fetchServer); _DBNameList = servObj.GetDatabases(serverSelected); }public RelayCommand serverCommand { get; set; } public void fetchServer(object server) { serverSelected = server.ToString(); }ModelClasspublic List<string> GetServers() { XmlDocument doc = new XmlDocument(); doc.Load(@"C:\Users\477340\Desktop\newFile.xml"); XmlNodeList nodes = doc.SelectNodes("/servers/server"); foreach (XmlNode node in nodes) { lstServerName.Add(node.Attributes["name"].InnerText); } return lstServerName; } public List<string> GetDatabases(string server) { XmlDocument doc = new XmlDocument(); doc.Load(@"C:\Users\477340\Desktop\newFile.xml"); XmlNodeList nodes = doc.SelectNodes("/servers/server"); foreach (XmlNode node in nodes) { if (node.Attributes["name"].InnerText == server) { XmlNodeList databases = node.SelectNodes("database"); foreach (XmlNode db in databases) { lstDBName.Add(db.InnerText); } } } return lstDBName; } 解决方案 Try these links -Cascading Dropdown & Multiselect Option in ListBox Using WPF[^]http://social.technet.microsoft.com/wiki/contents/articles/17946.cascading-comboboxes-in-wpf-using-mvvm.aspx[^] 这篇关于如何绑定级联组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-16 12:56