0

I've been trying to make a user control for a questionaire application where I have some questions that needs follow-up questions under certain conditions. A simple senario could be that we want to show follow-up quesions if we get a "yes" on some question.

At this point I have followed the example from Zenexer on a similar question. My thought was that I would put the first child of my user control in one container (a StackPanel or whatever) and all subsequent elements in a second StackPanel. But in the aforementioned example all child elements get stuffed into one element in the user control. To my understanding this is because [ContentProperty(nameof(Children))] is set to all of the content of the user control.

I tried to change the getter and setter of Children in the Zenexer's example but to no avail.

The Question: Is there a way to spilt the children of my user control into two (or more) elements in my user control with XAML that looks something like this:

MainWindow.xaml

<SubQuestionBox>
    <BinaryQuestion
            QuestionNumber="4.1"
            QuestionText="Parent question"/>

    <TextQuestion
            QuestionNumber="4.1.1"
            QuestionText="Child question 1"/>

    <TextQuestion
            QuestionNumber="4.1.2"
            QuestionText="Child question 2"/>
</SubQuestionBox>

SubQuestionBox.xaml

<UserControl x:Class="SubQuestionBox">

    <!--StackPanel To contain the question controls-->
    <StackPanel>
        <StackPanel x:Name="ParentContainer" />
        <StackPanel x:Name="SubQuestionsContainer" />
    </StackPanel>

</UserControl>
Community
  • 1
  • 1
Smedegaard
  • 727
  • 1
  • 6
  • 18
  • If there is a yes in response to the question in your UI then based on that property changed you can bind the data to another container. Read more and WPF bindings and MVVM and you should be able to construct your logic with that knowledge. – Versatile Sep 13 '16 at 14:20
  • @Versatile, that's not what I'm asking. I'm aware of events and data bindings. The question is about distributing the content of a user control into two containing elements. – Smedegaard Sep 14 '16 at 05:39
  • I did a work-around where I remove the "parent question" and insert it in the "Parent Container". I doesn't seem very elegant to me so if anyone have a better solution, please share. – Smedegaard Sep 14 '16 at 06:05

1 Answers1

0

If anyone wonders how I did it, it goes something like this.

SubQuestionBox.xaml is something like in the original question

SubQuestionBox.xaml.cs The Children property is a DependencyProperty like in the example of @Zenexer

public SubQuestionBox()
{
    InitializeComponent();
    Children = SubQuestionsContainer.Children;

    // add listener for Loaded event and call OnLoaded()     
    Loaded += OnLoaded; 
    if (ParentQuestion != null)
        // The BinaryQuestion has a AnswerChanged event
        ParentQuestion.AnswerChanged += ToggleCollapse;
}

public void DistributeQuestions()
{
    // This method seems super hacky to me. 
    // I would have thought there is a more elegant way

    UIElement parent = null;
    if (Children != null)
    {
        parent = Children[0];
        Children.RemoveAt(0);
    }
    if (ParentContainer != null && parent != null)
    {
        ParentContainer.Children.Add(parent);
    }

}

private void OnLoaded(object sender, RoutedEventArgs e)
{
    DistributeQuestions();
}
Community
  • 1
  • 1
Smedegaard
  • 727
  • 1
  • 6
  • 18