2016-05-31 43 views

回答

1

它就像这样简单,如果你不想存储代码背后的数据或什么的。

标签的内容财产只是绑定到这样的文本框中的文本财产Content="{Binding ElementName=TextBox1, Path=Text}"

检查下面的代码示例!

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

如果你想保存然后将数据内容和文本属性绑定到一个单一的物业,使双向结合。

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>