从许多动态添加的组合框中检索值

本文关键字:检索 组合 许多 动态 添加 | 更新日期: 2023-09-27 18:17:42

我必须实现一个应用程序,用户能够从未知数量的组合框中选择许多不同的值。如果用户单击"plus"按钮,将出现包含五个组合框的新行。如果按下"减号"键,最后一行将消失。

XAML:

<WrapPanel Margin="6,6,0,3">
    <Button Command="{Binding AddTextBoxRowCommand}">
        <Image Source="pack://application:,,,/Images/Dialogs/FI_Hinzufuegen_16x16.png" Width="16" Height="16" />
    </Button>
    <Button IsEnabled="{Binding IsRemoveButtonEnabled}" Command="{Binding RemoveTextBoxRowCommand}" Margin="6,0,0,0">
        <Image Source="pack://application:,,,/Images/Dialogs/FI_Loeschen_16x16.png" Width="16" Height="16" />
    </Button>
</WrapPanel>
<!-- Segment und Feld auswahl -->
<ItemsControl ItemsSource="{Binding TextBoxRowCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                <ComboBox SelectedItem="SelectedSegmentList1" ItemsSource="{Binding Path=SegmentList1}" DisplayMemberPath="SegmentName" SelectedValuePath="SegmentFile" />
                <ComboBox SelectedItem="SelectedFieldList1" ItemsSource="{Binding Path=FieldList1}" DisplayMemberPath="FieldName" SelectedValuePath="Type" />
                <ComboBox SelectedItem="SelectedOperationList" ItemsSource="{Binding Path=DZOperationList}" DisplayMemberPath="OpName" SelectedValue="OpType" />
                <ComboBox SelectedItem="SelectedSegmentList2"  ItemsSource="{Binding Path=SegmentList2}" DisplayMemberPath="SegmentName" SelectedValuePath="SegmentFile" />
                <ComboBox SelectedItem="SelectedFieldList2" ItemsSource="{Binding Path=FieldList2}" DisplayMemberPath="FieldName" SelectedValuePath="Type" />
            </WrapPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

ViewModel:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using DataAccessLayer.DirectoryParser;
using DataAccessLayer.Settings;
using Model;
using ViewModelBase;
namespace ViewModel
{
    public class ModalDialogValueViewModel : ViewModelBase.ViewModelBase
    {
        private RelayCommand addTextBoxRowCommand;
        private RelayCommand removeTextBoxRowCommand;
        private bool isRemoveButtonEnabled;
        private OperationModel opModel;
        private Settings settings;
        private List<Segment> segments;
        private List<Field> fields;
        /// <summary>
        /// Initialisiert das ModalDialogValueViewModel
        /// </summary>
        public ModalDialogValueViewModel()
        {
            settings = new Settings();
            opModel = new OperationModel();
            Segments = PKSegmentParser.GetSegments(settings.PKDirectory, "PROD");
            TextBoxRowCollection = new ObservableCollection<ObjectStructureHelper>();
            TextBoxRowCollection.Add(new ObjectStructureHelper(Segments, Fields, opModel.OpList, Segments, Fields));
            this.PropertyChanged += new PropertyChangedEventHandler(modalDialogValueViewModel_PropertyChanged);
        }
        public bool IsRemoveButtonEnabled
        {
            get { return isRemoveButtonEnabled; }
            set
            {
                if (isRemoveButtonEnabled != value)
                {
                    isRemoveButtonEnabled = value;
                    OnPropertyChanged("IsRemoveButtonEnabled");
                }
            }
        }
        public ObservableCollection<ObjectStructureHelper> TextBoxRowCollection { get; set; }
        public List<Segment> Segments
        {
            get { return segments; }
            set
            {
                if ((segments == null && value != null) || !segments.SequenceEqual(value))
                {
                    segments = value;
                    OnPropertyChanged("Segments");
                }
            }
        }
        public List<Field> Fields
        {
            get { return fields; }
            set
            {
                if ((fields == null && value != null) || !fields.SequenceEqual(value))
                {
                    fields = value;
                    OnPropertyChanged("Fields");
                }
            }
        }
        public ICommand AddTextBoxRowCommand
        {
            get
            {
                if (addTextBoxRowCommand == null)
                    addTextBoxRowCommand = new RelayCommand(p => ExecuteAddFieldRowCommand());
                return addTextBoxRowCommand;
            }
        }
        public ICommand RemoveTextBoxRowCommand
        {
            get
            {
               if (removeTextBoxRowCommand == null)
                   removeTextBoxRowCommand = new RelayCommand(p => ExecuteRemoveTextBoxRowCommand());
               return removeTextBoxRowCommand;
            }
        }
        /// <summary>
        /// Fügt eine Zeile mit für weitere Feld-Verknüpfungen hinzu
        /// </summary>
        private void ExecuteAddFieldRowCommand()
        {
            TextBoxRowCollection.Add(new ObjectStructureHelper(Segments, Fields, opModel.OpList, Segments, Fields));
            if (TextBoxRowCollection.Count > 1)
                IsRemoveButtonEnabled = true;
        }
        /// <summary>
        /// Entfernt eine Zeile von Feld-Verknüpfungen
        /// </summary>
        private void ExecuteRemoveTextBoxRowCommand()
        {
            var count = TextBoxRowCollection.Count;
            if (count > 1)
                TextBoxRowCollection.RemoveAt(count - 1);
            if (count == 2)
                IsRemoveButtonEnabled = false;
        }
        /// <summary>
        /// ModalDialogValueViewModel PropertyChanged EventHandler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void modalDialogValueViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "")
            {
            }
        }
    }
}

ObjectStructureHelper类

using System.Collections.Generic;
using DataAccessLayer;
using DataAccessLayer.DirectoryParser;
namespace Model
{
    public class ObjectStructureHelper
    {
        public ObjectStructureHelper(List<Segment> sList1, List<Field> fList1, List<DZOperation> opList, List<Segment> sList2, List<Field> fList2)
        {
            SegmentList1 = sList1;
            FieldList1 = fList1;
            DZOperationList = opList;
            SegmentList2 = sList2;
            FieldList2 = fList2;
        }
        public List<Segment> SegmentList1 { get; set; }
        public List<Field> FieldList1 { get; set; }
        public List<DZOperation> DZOperationList { get; set; }
        public List<Segment> SegmentList2 { get; set; }
        public List<Field> FieldList2 { get; set; }
    }
}

使用此代码示例,我能够添加动态组合框,但我不知道如何检索每个组合框的选定值。谁有办法解决我的问题?

从许多动态添加的组合框中检索值

如果您要从ViewModelBase.ViewModelBase中导出ObjectStructureHelper,请通过SelectedItem添加要绑定的ComboBoxes的属性。

public class ObjectStructureHelper : ViewModelBase.ViewModelBase
{
    public Segment     SelectedSegmentList1  // property implementation with propertychanged
    public Field       SelectedFieldList1    // property implementation with propertychanged
    public DZOperation SelectedOperationList // property implementation with propertychanged
    public Segment     SelectedSegmentList2  // property implementation with propertychanged
    public Field       SelectedFieldList2    // property implementation with propertychanged
}

和更新你的模板,像这样:

<DataTemplate>
    <WrapPanel>
         <ComboBox SelectedItem="{Binding SelectedSegmentList1}" ItemsSource="{Binding Path=SegmentList1}" DisplayMemberPath="SegmentName" />
         <ComboBox SelectedItem="{Binding SelectedFieldList1}" ItemsSource="{Binding Path=FieldList1}" DisplayMemberPath="FieldName" />
         <ComboBox SelectedItem="{Binding SelectedOperationList}" ItemsSource="{Binding Path=DZOperationList}" DisplayMemberPath="OpName" />
         <ComboBox SelectedItem="{Binding SelectedSegmentList2}"  ItemsSource="{Binding Path=SegmentList2}" DisplayMemberPath="SegmentName" />
         <ComboBox SelectedItem="{Binding SelectedFieldList2}" ItemsSource="{Binding Path=FieldList2}" DisplayMemberPath="FieldName" />
    </WrapPanel>
</DataTemplate>

在WPF中,我们通常使用数据元素而不是UI元素。因此,与其编写代码来动态添加ComboBox元素,不如让代码添加数据元素,然后提供一个包含ComboBoxDataTemplate

如果你声明了一个自定义类,它包含了填充ComboBox和管理其选择值所需的所有相关属性,那么你可以为该类型声明一个DataTemplate,它将在每次遇到你的一个类实例时呈现一个ComboBox:

<DataTemplate DataType="{x:Type YourPrefix:YourClass}">
    <ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" />
</DataTemplate>

然后你可以添加一些自定义类实例到一个集合和数据绑定到一个ItemsControl.ItemsSource属性,他们将呈现为一组ComboBox的实例:

<ItemsControl ItemsSource="{Binding CollectionOfCustomClassInstances}" />

像这样组织你的项目意味着你只需要将你的类的实例添加到集合中,而不是实际的ComboBox es,因此,当你想查看结果时,你已经拥有了所有的数据…只需查看类实例中SelectedItem属性的值,以找出每个ComboBox中选择的值。

请参阅MSDN上的数据绑定概述页以获取进一步帮助。