<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>
<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>
public class BackUpViewModel : MVVMSupport.ViewModel.ViewModelBase
{
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();
}
public 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;
}
当我选择Frist Combo Box中的任何服务器时,所选服务器应该传入模型类的GetDatabases(字符串服务器),并且在选择此服务器的基础上,应该在第二个组合框中填充Corresponind数据库。
答案 0 :(得分:0)
我遇到过类似的问题。 最后我通过简单地使用第一个组合框的选择更改事件来解决它(GetSubItems是自己实现的):
private void cbFirst_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//if (selIndex != null)
{
cbSecond.ItemsSource = null;
IEnumerable<string> str = GetSubItems(cbFirst.SelectedIndex);
cbSecond.ItemsSource = str;
}
}