-1

実行時に1つのリストボックスからエントリを選択して別のリストボックスに追加できるUIを作成する必要があります。これで、listbox1にコンボボックスとチェックボックスがアイテムとして含まれる場合があります。たとえば、値が「Q1、Q2、Q3、Q4」のQuarterというラベルの付いたコンボボックスをリストボックス1のアイテムとして追加し、その中のエントリQ1を選択して、[追加]ボタンをクリックすると、リストボックス2に追加されます。 。逆も可能です。これは実行時に可能である必要があります。コンボボックスとチェックボックスをアイテムとしてリストボックスに追加するにはどうすればよいですか?また、追加-削除ボタンについて、私が持っているコードが正しいかどうかを提案してください。

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);
    }
4

1 に答える 1

1

これは、ニーズに合ったWPFソリューションです。あなたがそれがあなたのために役立つかもしれないと私に言ったので、私はそれを投稿しています。これは、非常に限定された時代遅れのテクノロジであるWinFormsで達成することを期待できるものを大幅に上回っています。

これが私の画面での表示です。

ここに画像の説明を入力してください

データを表すためにいくつかの単純なViewModelを使用しています。

ListItemViewModel(「ベース」モデル):

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

BoolListItemViewModel(チェックボックス用):

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

SelectableListItemViewModel(ComboBoxesの場合):

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);
        }
    }
}

これは「メイン」ViewModelであり、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>

そして最後に、これはウィンドウコードビハインドであり、ViewModelをいくつかのアイテムで初期化するだけです。

   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ます。

fromMVVMLightfromWPFTutorial.netを使用していViewModelBaseます。私のコードをコピーしてに貼り付けて、結果を自分で確認することができます(上記のリンクからこれらの2つのクラスも必要になります)DelegateCommandFile -> New Project -> WPF Application

これを既存のwinformsアプリケーションに統合する必要がある場合は、ElementHost

于 2013-03-06T18:21:10.647 に答える