我想創建一個UserControl
來表示問卷,如下圖所示(忽略缺少樣式)。如何在XAML中構建問卷共享相同答案的調查問卷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
時,根據上面的示例,提供的問題不是答案。有誰知道我需要調整什麼?
'//答案複製到每個QuestionAndAnswers.'一部分觸發回調只能一次。在設計模式下它有一些數據要複製。它在啓動應用程序後添加''的答案後不運行。 –
ASh
@ASh如何在添加XAML的答案之後獲得答案的複製以運行?我試圖將該代碼放入Loaded回調函數中,但這沒有奏效。 –