views:

333

answers:

0

Let me explain the problem with an example:

[ContentProperty("Questions")]
public class QuestionForm : FrameworkElement
{
    public QuestionForm() { this.Questions = new Collection<Question>(); }
    public Collection<Question> Questions { get; set; }
}

public class Question : DependencyObject
{
    public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title",
       typeof(string), typeof(Question), new PropertyMetadata(null, null));
    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }
    // has other (dependency) properties
}

Now as you can see the QuestionForm type is a FrameworkElement type which has children of type Question. The Question type is a DependencyObject, with a number of properties. Now, In xaml I can code this easily, and it works:

<local:QuestionForm >
    <local:Question Title="This is Question 1?" />
    <local:Question Title="This is Question 2?" />
</local:QuestionForm>

However, if I try and do this it won’t work, obviously because the Question type is not part of the visual tree

<local:QuestionForm >
    <local:Question Title="{Binding Math.Q1}" />
    <local:Question Title="{Binding Math.Q2}" />
</local:QuestionForm>

    <local:QuestionForm >
    <local:Question Title="{Binding Biology.Q1}" />
    <local:Question Title="{Binding Biology.Q2}" />
    <local:Question Title="{Binding Biology.Q3}" />
</local:QuestionForm>

Here I can’t use an ItemsControl as the questions xaml is written into the page at design-time, each-time specifically as required – critically with databinding. So the problem is how to get the question type into the Visual Tree for the binding to work. Do I use a content control type with a template or somehow inject the question types into the visual tree? I have a faint idea that in WPF we use something called FrameworkContentElement, but this class is entirely absent in Silverlight?