アンケートを表現するためにUserControl
を作成したいと思っています。以下のようなものです(スタイリングの欠如を無視してください)。私は、だから私はQuestionnaireControl.xaml
QAMLでアンケートのUserControlを作成するにはどうすればよいでしょうか?
<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
<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>
として、XAMLでの重要な内容を指定できるようにしたい
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からの回答が追加された後にAnswersのコピーを取得するにはどうすればよいですか?私はそのコードをLoadedコールバックに入れようとしましたが、うまくいきませんでした。 –