C# 填充另一个组合框时自动添加组合框

C# 填充另一个组合框时自动添加组合框,c#,wpf,combobox,C#,Wpf,Combobox,嗨,我需要做一个程序,你必须添加一个未定义的元素数量到一个列表中,通过选择他们从一个组合框。我计划使用4个基本组合框,当用户从最后一个组合框中选择一个元素时,程序应自动在最后一个组合框下添加另一个组合框(我想使用stackpanel)。 我怎么办 <!-- Then show many of them--> <ListBox ItemsSource="{Binding Selections}"/> 谢谢 <!-- Then show many of them--&

嗨,我需要做一个程序,你必须添加一个未定义的元素数量到一个列表中,通过选择他们从一个组合框。我计划使用4个基本组合框,当用户从最后一个组合框中选择一个元素时,程序应自动在最后一个组合框下添加另一个组合框(我想使用stackpanel)。 我怎么办

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

<!-- Then show many of them-->
<ListBox ItemsSource="{Binding Selections}"/>
我的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>
<!-- Then show many of them-->
<ListBox ItemsSource="{Binding Selections}"/>

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

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

<!-- Then show many of them-->
<ListBox ItemsSource="{Binding Selections}"/>
具有选定值的集合,仅类似于

public class MyChoices
{
  public IEnumerable<string> Selections {get; set;}
}
<!-- Then show many of them-->
<ListBox ItemsSource="{Binding Selections}"/>
公共类MyChoices
{
公共IEnumerable选择{get;set;}
}
视图模型

<!-- Then show many of them-->
<ListBox ItemsSource="{Binding Selections}"/>
具有一个集合,该集合在您修改最后一项时立即扩展

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}"/>
公共类MyChoicesViewModel
{
公共MyChoiceViewModel()
{
选择=新的ObservableCollection();
//添加第一个空值
AddNewItem();
Selections.CollectionChanged+=(发件人,e)=>
{
//如果更改最后一个,请添加另一个
if(e.NewItems.Contains(Selections.Last()))
AddNewItem();
}; 
}
公共可观测集合选择{get;private set;}
public void AddNewItem()
{
var newItem=new ChoiceViewModel();
选择。添加(新项);
newItem.PropertyChanged+=()=>
{
//这是我们从ViewModel更新模型的地方
Model.Selections=选择中的x
选择x值;
}
}
}
公共类ChoiceViewModel:INotifyPropertyChanged
{
选择私有字符串;
选择公共字符串
{
获取{return\u selected;}
集合{
如果(_selected!=值)
{
_选择=价值;
OnPropertyChanged();
}
}
}
公共void OnPropertyChanged([CallerMemberName]字符串属性)
{
var temp=已更改的属性;
如果(温度!=null)
{
临时(此,新属性ChangedEventArgs(属性));
}
}
}
}
看法

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


您正在寻找一个新的解决方案。发布您当前的代码和XAML或所需的屏幕截图,或者请添加有关当前问题的更多具体信息。非常有用!非常感谢。代码并不精确,因为我还没有编译它,但它应该会给你工作的想法。使用提供属性更改跟踪集合(如ReactiveUI)的MVVM库也可能获得更好的里程数,但对于本例来说,这太过分了。