从一个文本框获取值到另一个XAML

本文关键字:获取 另一个 XAML 文本 一个 | 更新日期: 2023-09-27 18:11:07

我创建了一个文本框,当聚焦时,会弹出一个与其他文本框作为选项的弹出框。按下后会关闭弹出框。但我希望然后从我选择的文本框中获取值,并将其添加到原始文本框中。这可能吗?我是新来的,所以请原谅我!

这是目前为止我的代码。

 <Grid>
    <StackPanel Margin="0,51,0,-51">
        <TextBox  x:Name="text" GotKeyboardFocus="text_GotKeyboardFocus" Margin="158,0,169,0" Height="24" Text="Select Your Time..." />
        <Popup x:Name="popup" Width="282" Height="300" PlacementTarget="{Binding ElementName=text}">
            <Grid>
                <StackPanel>
                    <TextBox  x:Name="text2" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="1 second" />
                    <TextBox  x:Name="text3" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="2 second" />
                    <TextBox  x:Name="text4" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="3 second" />
                    <TextBox  x:Name="text5" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="4 second" />
                    <TextBox  x:Name="text6" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="5 second" />
                </StackPanel>
            </Grid>
        </Popup>
    </StackPanel>


</Grid>


 private void text_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        popup.IsOpen = true;
    }
    private void text_GotKeyboardFocus2(object sender, RoutedEventArgs e)
    {
        popup.IsOpen = false;
    }

从一个文本框获取值到另一个XAML

在xaml:

中获取从一个文本框到另一个文本框的值
<TextBox Name="txtBox1"/>
<TextBox Name="txtBox2" Text="{Binding ElementName=txtBox1, Path=Text,UpdateSourceTrigger=PropertyChanged}" />

我希望我明白你的意思。
只需将text.Text = (sender as TextBox).Text;添加到第二个事件处理程序中,如下所示:

private void text_GotKeyboardFocus2(object sender, RoutedEventArgs e)
{
     popup.IsOpen = false;
     text.Text = (sender as TextBox).Text;
}

好运。

从一个文本框到另一个文本框获取值

<TextBox Name="txtBox1"/>
<TextBox Name="txtBox2" Text="{Binding ElementName=txtBox1, Path=Text,UpdateSourceTrigger=PropertyChanged}" />