【问题标题】:Automatically add a combobox when you fill another one填充另一个组合框时自动添加一个组合框
【发布时间】:2013-12-05 01:37:28
【问题描述】:

您好,我需要制作一个程序,您必须通过从组合框中选择元素来将未定义数量的元素添加到列表中。我计划使用 4 个基本组合框,当用户从最后一个中选择一个元素时,程序应该自动在最后一个下添加另一个(我想使用堆栈面板)。 我该怎么办?

谢谢。

我的 XAML:

<StackPanel Name="listPanel" Grid.Column="0" Margin="10">
                        <Label Content="Example" FontWeight="Bold" HorizontalAlignment="Center"/>
                        <ComboBox Name="ex1Combobox" Margin="0,10,0,0"
                                  ItemsSource="{Binding ExList, Mode=TwoWay}"
                                  SelectedValue="{Binding SelectedEx}" 
                                  DisplayMemberPath="Name" 
                                  SelectedValuePath="ID"/>
                        <ComboBox Name="ex2Combobox" Margin="0,10,0,0"
                                  ItemsSource="{Binding ExList, Mode=TwoWay}"
                                  SelectedValue="{Binding SelectedEx}" 
                                  DisplayMemberPath="Name" 
                                  SelectedValuePath="ID"/>
                        <ComboBox Name="ex3Combobox" Margin="0,10,0,0"
                                  ItemsSource="{Binding ExList, Mode=TwoWay}"
                                  SelectedValue="{Binding SelectedEx}" 
                                  DisplayMemberPath="Name" 
                                  SelectedValuePath="ID"/>
                    </StackPanel>

【问题讨论】:

  • 您正在寻找ItemsControl。发布您当前的代码和 XAML 或您需要的屏幕截图,或者请添加有关您当前问题的更多具体信息。

标签: c# wpf combobox


【解决方案1】:

这是一个很好的例子,说明了为什么应该使用 MVVM。

型号

只有一些选定值的集合,例如

public class MyChoices
{
  public IEnumerable<string> Selections {get; set;}
}

视图模型

只要您修改最后一项,该集合就会扩展

public class MyChoicesViewModel
{
   public MyChoicesViewModel()
   {
     Selections = new ObservableCollection<ChoiceViewModel>();

     //Add first empty value
     AddNewItem();

     Selections.CollectionChanged += (sender, e) => 
     {
       // If you change the last add another
       if (e.NewItems.Contains(Selections.Last()))
         AddNewItem();

     }; 
   }

   public ObservableCollection<ChoiceViewModel> Selections {get; private set;}

   public void AddNewItem()
   {
     var newItem = new ChoiceViewModel();
     Selections.Add(newItem);
     newItem.PropertyChanged += () => 
      {
        //This is where we update the model from the ViewModel
        Model.Selections = from x in Selections
          select x.Value;
      }
    }
}

public class ChoiceViewModel : INotifyPropertyChanged
{
   private string _chosen;
   public string Chosen 
   {
     get { return _chosen; }
     set { 
           if (_chosen != value)
           {
             _chose = value;
             OnPropertyChanged();
           }
         }
    }
    public void OnPropertyChanged([CallerMemberName] string property)
    {
      var temp = PropertyChanged;
      if (temp != null)
      {
        temp(this, new PropertyChangedEventArgs(property));
      }
    }
 }
}

查看

<!-- Then show many of them-->
<ListBox ItemsSource="{Binding Selections}"/>

【讨论】:

  • 非常有用!谢谢!
  • 代码不准确,因为我没有编译它,但它应该给你工作的想法。您还可以使用提供属性更改跟踪集合的 MVVM 库(例如 ReactiveUI)获得更好的效果,但对于本示例来说这太过分了。
猜你喜欢
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-27
相关资源
最近更新 更多