我如何在列表框中添加combox和其他项目?

问题描述:

我需要创建一个UI,它允许我从一个列表框中选择条目并在运行时将其添加到另一个列表框中。现在,listbox1可能包含组合框和复选框作为项目。 例如,如果我在列表框1中添加一个标记为Quarter的组合框作为列表框1中的一个项目,然后单击“添加”按钮,它应该被添加到listbox2。反之亦然。这在运行时应该是可能的。我怎样才能将组合框和复选框作为一个项目添加到列表框中?此外,请建议如果对于添加删除按钮,我的代码是正确的。我如何在列表框中添加combox和其他项目?

private void MoveListBoxItems(ListBox source, ListBox destination) 
    { 
     ListBox.SelectedObjectCollection sourceItems = source.SelectedItems; 
     foreach (var item in sourceItems) 
     { 
      destination.Items.Add(item); 
     } 
     while (source.SelectedItems.Count > 0) 
     { 
      source.Items.Remove(source.SelectedItems[0]); 
     } 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     MoveListBoxItems(listBox1, listBox2); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     MoveListBoxItems(listBox2, listBox1); 
    } 
+2

我建议你适当地格式化你的问题,并发布一些代码。否则,你会得到积极报价,并可能让你的问题关闭。 – 2013-03-06 16:45:24

+0

你接受基于WPF的解决方案吗?你可以使用'ElementHost'将它集成到你现有的WinForms应用程序中。 – 2013-03-06 17:13:44

+0

是的,那应该没问题。但我不知道如何实现这一点。你能建议一个示例代码吗? – user2136634 2013-03-06 17:15:30

这是您的需求的WPF解决方案。我发布它是因为你告诉我这可能对你有用。它在很大程度上超越了你可以期望通过winform实现的任何事情,这是一种非常有限和过时的技术。

这是它的外观在我的屏幕上:

enter image description here

我使用一些简单的ViewModels来表示数据:

ListItemViewModel(以下简称 “基地” 之一):

public class ListItemViewModel: ViewModelBase 
    { 
     private string _displayName; 
     public string DisplayName 
     { 
      get { return _displayName; } 
      set 
      { 
       _displayName = value; 
       NotifyPropertyChange(() => DisplayName); 
      } 
     } 
    } 

BoolListItemViewModel(用于CheckBoxes):

public class BoolListItemViewModel: ListItemViewModel 
{ 
    private bool _value; 
    public bool Value 
    { 
     get { return _value; } 
     set 
     { 
      _value = value; 
      NotifyPropertyChanged(() => Value); 
     } 
    } 
} 

(对于组合框)SelectableListItemViewModel:

public class SelectableListItemViewModel: ListItemViewModel 
{ 
    private ObservableCollection<ListItemViewModel> _itemsSource; 
    public ObservableCollection<ListItemViewModel> ItemsSource 
    { 
     get { return _itemsSource ?? (_itemsSource = new ObservableCollection<ListItemViewModel>()); } 
    } 

    private ListItemViewModel _selectedItem; 
    public ListItemViewModel SelectedItem 
    { 
     get { return _selectedItem; } 
     set 
     { 
      _selectedItem = value; 
      NotifyPropertyChange(() => SelectedItem); 
     } 
    } 
} 

这是 “主” 视图模型,其保持2所列出和Commands(按钮动作)

public class ListBoxSampleViewModel: ViewModelBase 
    { 
     private ObservableCollection<ListItemViewModel> _leftItems; 
     public ObservableCollection<ListItemViewModel> LeftItems 
     { 
      get { return _leftItems ?? (_leftItems = new ObservableCollection<ListItemViewModel>()); } 
     } 

     private ObservableCollection<ListItemViewModel> _rightItems; 
     public ObservableCollection<ListItemViewModel> RightItems 
     { 
      get { return _rightItems ?? (_rightItems = new ObservableCollection<ListItemViewModel>()); } 
     } 

     private DelegateCommand<ListItemViewModel> _moveToRightCommand; 
     public DelegateCommand<ListItemViewModel> MoveToRightCommand 
     { 
      get { return _moveToRightCommand ?? (_moveToRightCommand = new DelegateCommand<ListItemViewModel>(MoveToRight)); } 
     } 

     private void MoveToRight(ListItemViewModel item) 
     { 
      if (item != null) 
      { 
       LeftItems.Remove(item); 
       RightItems.Add(item);  
      } 
     } 

     private DelegateCommand<ListItemViewModel> _moveToLeftCommand; 
     public DelegateCommand<ListItemViewModel> MoveToLeftCommand 
     { 
      get { return _moveToLeftCommand ?? (_moveToLeftCommand = new DelegateCommand<ListItemViewModel>(MoveToLeft)); } 
     } 

     private void MoveToLeft(ListItemViewModel item) 
     { 
      if (item != null) 
      { 
       RightItems.Remove(item); 
       LeftItems.Add(item);  
      } 
     } 
    } 

这是整个XAML对于窗口:

<Window x:Class="WpfApplication4.Window14" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfApplication4" 
     Title="Window14" Height="300" Width="300"> 
    <Window.Resources> 
     <DataTemplate DataType="{x:Type local:ListItemViewModel}"> 
      <TextBlock Text="{Binding DisplayName}"/> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type local:BoolListItemViewModel}"> 
      <CheckBox Content="{Binding DisplayName}" IsChecked="{Binding Value}" HorizontalAlignment="Left"/> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type local:SelectableListItemViewModel}"> 
      <ComboBox ItemsSource="{Binding ItemsSource}" SelectedItem="{Binding SelectedItem}" 
         HorizontalAlignment="Stretch" MinWidth="100"/> 
     </DataTemplate> 
    </Window.Resources> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition Width="100"/> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 
     <ListBox ItemsSource="{Binding LeftItems}" 
       x:Name="LeftList"/> 

     <StackPanel Grid.Column="1" VerticalAlignment="Center"> 
      <Button Content="Move to Right" 
        Command="{Binding MoveToRightCommand}" 
        CommandParameter="{Binding SelectedItem,ElementName=LeftList}"/> 
      <Button Content="Move to Left" 
        Command="{Binding MoveToLeftCommand}" 
        CommandParameter="{Binding SelectedItem,ElementName=RightList}"/> 
     </StackPanel> 

     <ListBox ItemsSource="{Binding RightItems}" 
       Grid.Column="2" x:Name="RightList"/> 
    </Grid> 
</Window> 

最后,这是Window Code-behind,其中仅初始化视图模型的一些项目:

public partial class Window14 : Window 
    { 
     public Window14() 
     { 
      InitializeComponent(); 

      DataContext = new ListBoxSampleViewModel() 
           { 
            LeftItems = 
             { 
              new ListItemViewModel(){DisplayName = "Item1"}, 
              new BoolListItemViewModel() {DisplayName = "Check Item 2", Value = true}, 
              new SelectableListItemViewModel() 
               { 
                ItemsSource = 
                 { 
                  new ListItemViewModel() {DisplayName = "Combo Item 1"}, 
                  new BoolListItemViewModel() {DisplayName = "Check inside Combo"}, 
                  new SelectableListItemViewModel() 
                   { 
                    ItemsSource = 
                     { 
                      new ListItemViewModel() {DisplayName = "Wow, this is awesome"}, 
                      new BoolListItemViewModel() {DisplayName = "Another CheckBox"} 
                     } 
                   } 
                 } 
               } 
             } 
           }; 
     } 
    } 

乍一看,这似乎是代码的很多...但如果你需要2秒来分析它...它只是“简单的,简单的性质和INotifyPropertyChanged。这就是你在WPF中编程的方式。

我正在谈论一个完全不同的范例,你可能会习惯在winforms中使用它,但是真的值得努力学习它。请注意,我的代码中没有任何地方与UI元素进行交互。我只是创建了ViewModel结构,并让WPF Binding System负责为我生成UI,使用提供的DataTemplates

我使用的从MVVM LightDelegateCommandWPFTutorial.net。您可以将我的代码复制并粘贴到File -> New Project -> WPF Application中,并自己查看结果(您还需要上述链接中的这两个类)

如果您需要将其集成到现有的Winforms应用程序中,您将需要ElementHost