首页 > 解决方案 > 在列表列表中查找选中项目的列表

问题描述

假设我有一个问卷应用程序,它由一个 ItemsControl 和一个控件列表组成,每个控件由一个 Label 和一个 ListBox 组成。每个 ListBox 中的项目是复选框或单选按钮或其他任何东西。

我的问题是:选中复选框时,如何确定复选框适用于哪个问题?我应该在 Tag 属性中引用问题吗?如果是这样,我该怎么做?

下面的标签绑定代码不起作用。它绑定到 ListBoxItem。如何将它绑定到 ItemsControl 项?

MainWindow.xaml:

<Window x:Class="ListWithinListTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="800">
  <Window.Resources>
    <ResourceDictionary>
      <Style x:Key="ConditionCheckBoxListStyle" TargetType="{x:Type ListBox}">
        <Setter Property="SelectionMode" Value="Multiple" />
        <Setter Property="ItemContainerStyle">
          <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}" >
              <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
              <Setter Property="Template">
                <Setter.Value>
                  <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <CheckBox IsChecked="{Binding IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
                              Click="CheckBoxClicked"
                              Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                              >
                      <ContentPresenter></ContentPresenter>
                    </CheckBox>
                  </ControlTemplate>
                </Setter.Value>
              </Setter>
            </Style>
          </Setter.Value>
        </Setter>
      </Style>
    </ResourceDictionary>
  </Window.Resources>

  <Grid>
    <ItemsControl Name="QuizControl" ItemsSource="{Binding QuizQuestions}" ScrollViewer.CanContentScroll="False">
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <StackPanel Margin="10 0 10 10" VerticalAlignment="Top">
            <Label Content="{Binding Text}" />
            <ListBox ItemsSource="{Binding Options}"
                     DisplayMemberPath="Text"
                     Tag="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                     Style="{StaticResource ConditionCheckBoxListStyle}"
                     />
          </StackPanel>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
  </Grid>
</Window>

MainWindow.xaml.cs:

using System.Windows;
using System.Windows.Controls;

namespace ListWithinListTest
{
    public class Option
    {
        public string Text { get; set; }
        public bool IsSelected { get; set; } = false;
    }

    public class Question
    {
        public string Text { get; set; }
        public Option[] Options { get; set; }
    }

    public class ViewModel
    {
        public Question[] QuizQuestions { get; set; }

        public ViewModel()
        {
            QuizQuestions = new Question[] {
                new Question { Text = "How are you?", Options = new Option[] { new Option { Text = "Good" }, new Option { Text = "Fine" } } },
                new Question { Text = "How's your dog?", Options = new Option[] { new Option { Text = "Sleepy" }, new Option { Text = "Hungry" } } },
            };
        }
    }

    public partial class MainWindow : Window
    {
        private ViewModel viewModel;

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = viewModel = new ViewModel();
        }

        private void CheckBoxClicked(object sender, RoutedEventArgs e)
        {
            Question question = viewModel.QuizQuestions[???];
        }
    }
}

标签: wpfxaml

解决方案


您已将 QuizQuestions 绑定到 QuizControl,您可以从 ItemsSource 属性中取回它。

var  questions = (Question[]) QuizControl.ItemsSource;

编辑

看起来你自己得到了答案,这只是我想对你原来的问题提出的另一种方式:

为您的 Option 类再创建一个属性

public class Option
{
    public string Text { get; set; }
    public bool IsSelected { get; set; } = false;
    public int Index{ get; set; }
}

然后将索引添加到您的每个问题选项中。

QuizQuestions = new Question[] {
            new Question { Text = "How are you?", Options = new Option[] { new Option { Text = "Good", Index = 0 }, new Option { Text = "Fine", Index = 0 } } },
            new Question { Text = "How's your dog?", Options = new Option[] { new Option { Text = "Sleepy", Index = 1 }, new Option { Text = "Hungry", Index = 1 } } },
};

在您的 CheckBox 事件中,您可以获得选项索引

private void CheckBoxClicked(object sender, RoutedEventArgs e)
{
    var s = (CheckBox)sender;
    var op = (Option)s.Tag;
    Question question = viewModel.QuizQuestions[op.Index];
}

推荐阅读