我有一个自定义的UserControl,其中包含自定义对象的集合。
public class Question : FrameworkElement
{
public readonly static DependencyProperty FullNameProperty =
DependencyProperty.Register("FullName", typeof(string), typeof(Question));
public readonly static DependencyProperty ShortNameProperty =
DependencyProperty.Register("ShortName", typeof(string), typeof(Question));
public readonly static DependencyProperty RecOrderProperty =
DependencyProperty.Register("RecOrder", typeof(int), typeof(Question));
public readonly static DependencyProperty AnswerProperty =
DependencyProperty.Register("Answer", typeof(string), typeof(Question));
public string FullName
{
get { return (string)GetValue(FullNameProperty); }
set { SetValue(NameProperty, value); }
}
public string ShortName
{
get { return (string)GetValue(ShortNameProperty); }
set { SetValue(ShortNameProperty, value); }
}
public string Answer
{
get { return (string)GetValue(AnswerProperty); }
set { SetValue(AnswerProperty, value); }
}
public int RecOrder
{
get { return (int)GetValue(RecOrderProperty); }
set { SetValue(RecOrderProperty, value); }
}
}
在我的控制代码后台,我有以下内容:
public readonly static DependencyProperty QuestionsProperty =
DependencyProperty.Register("Questions", typeof(ObservableCollection<Question>), typeof(FormQuestionReportViewer),
new PropertyMetadata(new ObservableCollection<Question>()));
public ObservableCollection<Question> Questions
{
get { return GetValue(QuestionsProperty) as ObservableCollection<Question>; }
set { SetValue(QuestionsProperty, value); }
}
在XAML标记中,我可以这样定义我的控件:
<custom:CustomControl>
<custom:CustomControl.Questions>
<custom:Question FullName="smth text" ShortName="smth text" RecOrder="1" Answer="Yes" />
<custom:Question FullName="smth text" ShortName="smth text" RecOrder="2" Answer="Yes" />
</custom:CustomControl.Questions>
</custom:CustomControl>
它的功能很好,但我希望能像这样在XAML中绑定我的集合属性
<custom:CustomControl>
<custom:CustomControl.Questions Items="{binding Path=Questions}">
<custom:Question FullName="{binding Name}" ShortName="{binding ShortName}" RecOrder="{binding RecOrder}" Answer={binding Answer}" />
</custom:CustomControl.Questions>
</custom:CustomControl>
我该如何进行绑定?