【问题标题】:Child combo box showing values other than the parents combo box selected value's item子组合框显示父组合框所选值项目以外的值
【发布时间】:2015-11-13 05:09:03
【问题描述】:

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, Mode=OneWay}"   SelectedItem="{Binding Path=serverSelected,  Mode=OneWay, 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" SelectedItem="{Binding Path= fetchServer, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></ComboBox>

查看模型

父组合框:_ServerNameList 子组合框是:_DBNameList

ViewModel 构造函数

     public BackUpViewModel()
            {                       
                BackUpContext servObj = new BackUpContext();
                _ServerNameList = servObj.GetServers();     

                serverCommand = new RelayCommand(fetchServer);
            }

public RelayCommand serverCommand { get; set; }
public void fetchServer(object server)
            {
                 BackUpContext db= new BackUpContext();            
                 serverSelected = server.ToString();
                _DBNameList = db.GetDatabases(serverSelected);           
            }

当我在父组合框中选择第一个项目时,子组合框为空白。之后,当我在父组合框中选择第二个项目时,子项显示与父项中的第一个项目相关的项目。当我在 Parent 中选择第三个项目时,孩子会在父组合框中显示与第二个项目相关的项目。

我不知道为什么会这样。有什么建议吗?

【问题讨论】:

  • 能否请包含您的 xaml?
  • @tagaPdyk:
  • @tagaPdyk: 组合框>
  • 请编辑您的问题并在此处添加 xaml。这里的问题是您的“第一个”选择,对吗?
  • @tagaPdyk : XAML 已添加到问题中。

标签: c# wpf combobox


【解决方案1】:

您必须为第二个 ComboBox 设置值。

public BackUpViewModel()
{                       
    BackUpContext servObj = new BackUpContext();
   _ServerNameList = servObj.GetServers();     

   // Select the first.
   fetchServer(_ServerNameList.FirstOrDefault());
}

【讨论】:

  • 不。它对我不起作用...数据库值未获取所需的选定值
  • Hmmm.. 同样,在你的 Vm 初始化期间,你的 dblist 应该是什么值?
  • @tagaPdyk :最初两个框都是空白的,它应该是这样的。但是问题来了,当我单击服务器框并选择服务器框中的任何值时,数据库框仍然是空的,它不应该是空的,它应该显示该选定值的项目。现在,当我再次在服务器框中选择另一个值时,数据库列表会填充但选择的值项错误。
  • 例如:a : 1,2,3 b: 4,5 c: 6,7,8 现在当我在服务器中选择任何值时,数据库框为空。当我选择 b-> 它给出 123, C-> 给出 4,5 , a-> 6,7,8
猜你喜欢
  • 2013-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多