【问题标题】:SelectionChanged Combobox WPFSelectionChanged 组合框 WPF
【发布时间】:2015-10-11 12:29:43
【问题描述】:

我想根据选择的其他ComboBox 填充ComboBox。 两个组合框都是使用 WCF 从数据库中填充的。 我的问题是,在第一次选择时它不起作用(在第二次选择之后它起作用并且它显示第一次选择的结果)。

XAML

<ComboBox 
   x:Name="selClientCombo" 
   SelectionChanged="listEchipamente" 
   IsEditable="True" 
   SelectedIndex="-1" 
   HorizontalAlignment="Left" 
   Margin="455,35,0,0" 
   VerticalAlignment="Top" 
   Width="215" 
   ItemsSource="{Binding Mode=OneWay}"/>
<ComboBox 
   x:Name="selEchipamentCombo" 
   HorizontalAlignment="Left" 
   Margin="457,65,0,0" 
   VerticalAlignment="Top" 
   Width="213" 
   ItemsSource="{Binding}"/>

代码

  private void listEchipamente(object sender, SelectionChangedEventArgs e)
        {
            List<string> echipamenteWCF = client.getEchipament(selClientCombo.Text).ToList();

            MessageBox.Show(" Client Selected !");
            if (selEchipamentCombo.Items.Count >0)
            {
                selEchipamentCombo.Items.Clear();
            }
                for (int i = 0; i < echipamenteWCF.Count(); i++)
                {
                    selEchipamentCombo.Items.Add(echipamenteWCF[i]);
                }

            }

【问题讨论】:

    标签: c# wpf wcf xaml combobox


    【解决方案1】:

    SelectionChanged 被触发时,Text 尚未更新(因此它保留了之前的值)。

    您应该访问底层数据项来获取文本:

    if(selClientCombo.SelectedItem == null) return;
    List<string> echipamenteWCF = 
                       client.getEchipament(selClientComo.SelectedItem.ToString()).ToList();
    ...
    

    我认为ToString() 将解析显示文本。您始终可以将SelectedItem 转换为实际类型并轻松访问其字符串属性(显示为文本)。您还可以访问SelectedValue,条件是为ComboBox 设置一些SelectedValuePath

    【讨论】:

      猜你喜欢
      • 2018-05-03
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      • 2013-04-03
      相关资源
      最近更新 更多