0

So i have a Textbox and a Label. if I write something in the Textbox I would like the Label to show instaltly the text that I am currently writing in the Textbox.

Devid Demetz
  • 125
  • 6

2 Answers2

1

It is as simple as this if you don't want to store the data in code behind or something.

Just bind the Content property of the label to the Text property of the text box like this : Content="{Binding ElementName=TextBox1, Path=Text}"

Check sample code below!

<Window x:Class="TestApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <TextBox x:Name="TextBox1" HorizontalAlignment="Left" Height="23" Margin="203,109,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label Content="{Binding ElementName=TextBox1, Path=Text}" HorizontalAlignment="Left" Margin="203,172,0,0" VerticalAlignment="Top" Width="120" />

    </Grid>
</Window>

If you want to save the data then bind both Content and Text property to a single Property and enable TwoWay binding.

ViVi
  • 4,339
  • 8
  • 29
  • 52
0
<Window x:Class="StackOverflowWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:StackOverflowWPF"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <Label Margin="15" x:Name="lbl"></Label>
    <TextBox Width="220" Height="40" VerticalContentAlignment="Center" FontSize="14" x:Name="txtBox" 
             Text="{Binding ElementName=lbl, Path=Content, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</Window>
EngineerSpock
  • 2,575
  • 4
  • 35
  • 57