2014-06-14 3 views
0

Windows 스토어에 응용 프로그램을 쓰고 있습니다. 답변을 드릴 수있는 연습 문제가 있습니다. 나는 클래스를 만들었고 여기에는 2 개의 변수 questionanswer이 있습니다. 질문의문자열 사이에 TextBox 삽입

예 :

Question q = new Question(
    "This is simple [1] of question. This is the [2]", 
    new string[] 
    { 
     "sample", 
     "end" 
    }); 

내가 원하는 질문과 TextBlock[1]의 장소에서 TextBox (우리가 답을 쓸 것입니다 장소) 같은 그리드보기로 질문을 추가하는 것입니다 및 [2]. 그래서 다음과 같이 보일 것입니다 :

<TextBox> 
    <TextBlock> This is simple </TextBlock> 
<TextBox/> 

<TextBlock> of question. This is the </TextBlock><TextBox/> 

나는 이렇게 보일 수 있을지 잘 모르겠습니다. 내가 제시 한 방식으로 MainPage.xaml에 항목을 추가하는 Question 클래스에 메소드를 만들 수 있습니까?

답변

0

질문을 표시하려면 UserControl을 직접 작성해야한다고 생각합니다. 요점은 코드 뒤에 동적으로 컨트롤 (TextBlock/TextBox)을 만들고이를 어린이로 WrapPanel에 추가하는 것입니다. 여기서 XAML 만 단순한 결정을 볼 수는 없습니다.

XAML : 뒤에

<UserControl <!--skipped--> > 
    <WrapPanel x:Name="mainPanel"> 
    </WrapPanel> 
</UserControl> 

코드 :

partial class QuestionControl : UserControl 
{ 
    public QuestionControl() 
    { 
     InitializeComponent(); 
    } 

    private Question _question = null; 
    public Question Question 
    { 
     get { return _question; } 
     set 
     { 
      _question = value; 
      UpdateUI(); 
     } 
    } 

    private void UpdateUI() 
    { 
     mainPanel.Children.Clear(); 

     if (this.Question != null) 
     { 
      List<FrameworkElement> controls = new List<FrameworkElement>(); 
      string[] questionSegments = // some code to split question here 

      foreach (var qs in questionSegments) 
      { 
       controls.Add(new TextBlock() { Text = qs }); 
      } 

      for (int i = 0; i < this.Question.AnswerStrings.Length; i++) 
      { 
       string answer = this.Question.AnswerStrings[i]; 
       TextBox newTextBox = new TextBox(); 
       controls.Insert(i * 2 + 1, newTextBox); // inserting TextBoxes between TextBlocks 
      } 

      foreach (var control in controls) 
      { 
       mainPanel.Children.Add(control); // adding all the controls to the wrap panel 
      } 
     } 
    } 
} 
+0

정말 고마워요 @AlexSkiba. WrapPanel을 StackPanel로 변경하면 완벽하게 작동합니다. – Becon