在列表列表中查找选中项目的列表

时间:2018-12-16 22:36:54

标签: wpf xaml

比方说,我有一个问卷调查应用程序,它由一个ItemsControl和一个控件列表组成,每个控件列表均由Label和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[???];
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您已将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];
}

答案 1 :(得分:0)

好吧,我搞砸了Live Visual Tree窗口,直到通过消除过程,我意识到ListBox是问题将要绑定的对象。我知道这是一个很大的问题,但这就是我的想法。然后,我使用RelativeSource AncestorType进行查找,并使用DataSource属性来获取问题:

MainWindow.xaml:

                <CheckBox IsChecked="{Binding IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
                          Click="CheckBoxClicked"
                          Tag="{Binding RelativeSource={RelativeSource AncestorType=ListBox}}"
                          >

MainWindow.xaml.cs:

    private void CheckBoxClicked(object sender, RoutedEventArgs e)
    {
        CheckBox checkBox = (CheckBox)sender;
        ListBox listBox = (ListBox)checkBox.Tag;
        Question question = (Question)listBox.DataContext;
        Debug.WriteLine(question.Text);
    }