将文本从一个堆栈面板传输到另一个堆栈面板

时间:2017-12-15 10:24:03

标签: c# xaml uwp

我正在开发一个UWP待办事项列表。

我有两个并排的堆叠面板。

<Grid x:Name="MainGrid" Background="Transparent">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition  />
        <RowDefinition  />
        <RowDefinition MaxHeight="65"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0" Grid.ColumnSpan="2" >
        <TextBlock Margin="10" FontSize="30">Please enter in an item you wish to complete</TextBlock>
        <StackPanel Orientation="Horizontal">
            <TextBox MinWidth="200" Name="Input"></TextBox>
            <Button Margin="10" Click="ButtonAdd_Click">Add</Button>
        </StackPanel>
        <TextBlock Margin="10" FontSize="30">Below are the Items you need to complete</TextBlock>
    </StackPanel>

    <StackPanel x:Name="outputArea" Grid.Column="0" Grid.Row="1" >

    </StackPanel>

    <StackPanel x:Name="completedList" Grid.Column="1" Grid.Row="1">

    </StackPanel>
    <Button Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left"  Click="ButtonSettings_Click">Settings</Button>
    <Button Grid.Column="1" Grid.Row="2" HorizontalAlignment="Right"  Click="ButtonClear_Click">Clear done</Button>
</Grid>
</Page>

outputArea stackpanel包含用户输入的文本 C#代码

private void ButtonAdd_Click(object sender, RoutedEventArgs e)
    {
        StackPanel sp = new StackPanel(); ;
        TextBox textbox = new TextBox();
        textbox.Text += Input.Text;
        textbox.IsReadOnly = true;
        CheckBox done = new CheckBox();
        done.Content = "check when done";
        done.Checked += Done_Checked;

        sp.Orientation = Orientation.Horizontal;
        sp.Children.Add(textbox);
        sp.Children.Add(done);



        outputArea.Children.Add(sp);

    }

此代码从文本框获取输入并将其输出到输出区域 我的问题是,当勾选复选框时,我试图将文本从输出区域移动到完成的列表。我试过以下

 private void Done_Checked(object sender, RoutedEventArgs e)
    {
        // find the parent horiz sp, and move it.
        TextBox textbox = new TextBox();

        textbox.Text += textbox.Text;
        textbox.IsReadOnly = true;
        StackPanel stackpanel = new StackPanel();

        stackpanel.Orientation = Orientation.Horizontal;

        stackpanel.Children.Add(textbox);

    }

提前致谢。

1 个答案:

答案 0 :(得分:0)

这将有效:

private void Done_Checked(object sender, RoutedEventArgs e)
{
    StackPanel sp = (StackPanel)VisualTreeHelper.GetParent((CheckBox)sender);

    outputArea.Children.Remove(sp);
    completedList.Children.Add(sp);

}