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

时间:2015-09-03 15:35:56

标签: c# wpf xaml textbox popup

我创建了一个文本框,当聚焦时会弹出一个带有其他文本框的弹出框作为选项..当按下时关闭弹出框...但是我喜欢那个然后拿下来自我选择的任何文本框的值并添加到原始文本框中。这可能吗?我是wpf的新手,请原谅我!

到目前为止,这是我的代码。

 <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;
    }

3 个答案:

答案 0 :(得分:2)

在xaml中从一个文本框获取值到另一个文本框:

<TextBox Name="txtBox1"/>

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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

<TextBox Name="txtBox1"/>

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