【发布时间】:2017-06-16 19:14:49
【问题描述】:
我想建立一个UserControl 来代表一个问卷,如下图所示(忽略缺少样式)。
我希望能够在 XAML 中指定重要的内容,比如
<local:QuestionnaireControl>
<local:QuestionnaireControl.Questions>
<local:QuestionAndAnswers Number="1" Question="Is this working?" />
<local:QuestionAndAnswers Number="2" Question="Are these questions sharing answers?" />
</local:QuestionnaireControl.Questions>
<local:QuestionnaireControl.Answers>
<local:Answer Value="0" Text="Yes" />
<local:Answer Value="1" Text="No" />
<local:Answer Value="2" Text="Help Me Please" />
</local:QuestionnaireControl.Answers>
</local:QuestionnaireControl>
所以我有以下QuestionnaireControl.xaml
<UserControl x:Class="MyProject.QuestionnaireControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:local="clr-namespace:MyProject"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
d:DataContext="{d:DesignInstance Type=local:QuestionnaireControl, IsDesignTimeCreatable=True}">
<ItemsControl ItemsSource="{Binding Questions}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Number, StringFormat='{}{0}.'}" Margin="0,0,10,0" />
<TextBlock Text="{Binding Question}" Width="220"/>
<ItemsControl ItemsSource="{Binding Answers}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<RadioButton
Content="{Binding Text}"
IsChecked="{Binding IsSelected}"
GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=local:QuestionAndAnswers}, Path=Question}"
Margin="0,0,10,0"
/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</UserControl>
还有下面QuestionnaireControl.xaml.cs
public partial class QuestionnaireControl : UserControl
{
public QuestionnaireControl()
{
InitializeComponent();
if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
Questions = new List<QuestionAndAnswers> {
new QuestionAndAnswers() { Number=1, Question="Do you like pizza?" },
new QuestionAndAnswers() { Number=2, Question="Can you surf?" },
new QuestionAndAnswers() { Number=3, Question="Are you funny?" },
new QuestionAndAnswers() { Number=4, Question="Is Monday your favorite day of the week?" },
new QuestionAndAnswers() { Number=5, Question="Have you been to Paris?" },
new QuestionAndAnswers() { Number=6, Question="When sleeping, do you snore?" },
new QuestionAndAnswers() { Number=7, Question="Could you be living in a dream?" }
};
Answers = new List<Answer> {
new Answer() { Value=1, Text="Yes", IsSelected=false },
new Answer() { Value=2, Text="No", IsSelected=false },
new Answer() { Value=3, Text="Sort Of", IsSelected=false },
};
}
else
{
Questions = new List<QuestionAndAnswers>();
Answers = new List<Answer>();
}
// Copy Answers to each QuestionAndAnswers.
foreach (QuestionAndAnswers qa in Questions)
{
qa.Answers = new List<Answer>(Answers);
}
}
public List<QuestionAndAnswers> Questions
{
get { return (List<QuestionAndAnswers>)GetValue(QuestionsProperty); }
set { SetValue(QuestionsProperty, value); }
}
public static readonly DependencyProperty QuestionsProperty =
DependencyProperty.Register("Questions", typeof(List<QuestionAndAnswers>), typeof(QuestionnaireControl), new FrameworkPropertyMetadata(new List<QuestionAndAnswers>()));
public List<Answer> Answers
{
get { return (List<Answer>)GetValue(AnswersProperty); }
set { SetValue(AnswersProperty, value); }
}
public static readonly DependencyProperty AnswersProperty =
DependencyProperty.Register("Answers", typeof(List<Answer>), typeof(QuestionnaireControl), new FrameworkPropertyMetadata(new List<Answer>()));
}
public class QuestionAndAnswers
{
public int Number { get; set; }
public string Question { get; set; }
public List<Answer> Answers { get; set; }
}
public class Answer
{
public string Text { get; set; }
public int Value { get; set; }
public bool IsSelected { get; set; }
}
使用上面的代码,我可以在 Visual Studio 设计器中生成上面的 QuestionnaireControl 图像。但是,当我实际使用QuestionnaireControl 时,根据上面的示例,会呈现问题但不会呈现答案。有谁知道我需要调整什么?
【问题讨论】:
-
// Copy Answers to each QuestionAndAnswers.部分只工作一次。在设计模式下,它有一些数据要复制。在启动应用程序后添加来自<local:QuestionnaireControl.Answers>的答案后,它不会运行。 -
@ASh 在添加来自 XAML 的答案后,如何复制答案以运行?我尝试将该代码放入 Loaded 回调中,但没有奏效。
标签: c# wpf xaml user-controls