2016-05-31 19 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> 
相關問題