【问题标题】:WPF Combobox DataBindWPF 组合框数据绑定
【发布时间】:2012-11-05 14:44:15
【问题描述】:

我有两个组合框。第一个组合框用于选择经理,第二个组合框用于选择助理。经理。但是,两个组合框中的源名称相同。因此,例如,如果我从第一个组合框中选择“James”,那么我不会从第二个组合框中选择它。当我在第二个组合框中单击“James”时,它应该给我一个错误,并且不能选择“JAmes”。

我将该代码写入第二个组合框的 selection_changed 事件:

if (Manager.SelectedItem == Asst_MAnager.SelectedItem)
{
    MessageBox.Show("You must change Asst_Manager");
} 

没错,如果我选择相同的项目,那么它会给我错误消息。但是,它仍然在错误消息后选择相同的项目。我的 WPF 代码如下。能不能给点意见?

<local:ComboBoxCW Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3" x:Name="Manager" Text="" Background="#FFC8D2E8"  Margin="0,0,0,3"
            SelectedID="{Binding Path=[Manager}"  />
<local:ComboBoxCW Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" x:Name="Asst_Manager" Text="" Background="#FFC8D2E8" CWListName="Assistant Manager" Margin="0,0,0,3"
            SelectedID="{Binding Path=[Asst_Manager]}" SelectionChanged="Asst_Manager_SelectionChanged" />

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    由于您的组合框是数据绑定的,因此您不能将 SelectedIndex 设置为 -1(通常应该取消选择您所做的任何选择),并且看起来像这样:

    if (Manager.SelectedItem == Asst_MAnager.SelectedItem)
    {
        MessageBox.Show("You must change Asst_Manager");
        Asst_Manager.SelectedIndex = -1;
    } 
    

    因此,我建议您在每个框中的第一项中为“选择名称”。这样你就可以做到:

    if (Manager.SelectedItem == Asst_MAnager.SelectedItem)
    {
        MessageBox.Show("You must change Asst_Manager");
        Asst_Manager.SelectedIndex = 0;
    } 
    

    或类似的东西。这并不漂亮,而且到目前为止也不是最好的方法。但它很简单,并且可以完成工作。

    【讨论】:

    • 这是一个非常简单的解决方案 :) 但我喜欢它
    • @Isi:已编辑。你是这个意思?
    【解决方案2】:

    您是否尝试过使用验证规则?您可以使用从 ValidationRule 继承的类并在选择值之前检查那里的值。

    Here你有一篇文章解释了这个话题。希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      您可以使用选择更改事件之类的

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {
              string s = string.Empty;
              string s1 =string.Empty;
              if (comboBox2.SelectedItem != null)
              {
                  s1 = comboBox2.SelectedItem.ToString();
              }
              if (comboBox1.SelectedItem != null)
              {
                  s = comboBox1.SelectedItem.ToString();
              }
              if (s == s1)
              {
                  MessageBox.Show("You have Selected These Item As Second Combobox");
                  comboBox1.SelectedItem = null;
              }
          }
      
          private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {
              string s=string.Empty;
              string s1 = string.Empty;
              if (comboBox1.SelectedItem != null)
              {
                  s = comboBox1.SelectedItem.ToString();
              }
              if (comboBox2.SelectedItem != null)
              {
                  s1 = comboBox2.SelectedItem.ToString();
              }
              if (s == s1)
              {
                  MessageBox.Show("You have Selected These Item As First Combobox");
                  comboBox2.SelectedItem = null;
              }
          }
      

      【讨论】:

        【解决方案4】:

        我只会使用 LINQ 过滤 Asst_Manager 列表,甚至在 asst mgr 列表中都没有经理名称。在从第一个列表中选择一个项目之前,我不会激活第二个列表。

        【讨论】:

          猜你喜欢
          • 2015-12-12
          • 2014-02-03
          • 2011-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-26
          • 2020-02-14
          相关资源
          最近更新 更多