(一) WPF中TextBox控件与TextBlock控件值的绑定

来源:互联网 发布:淘宝兑换虾米会员 编辑:程序博客网 时间:2024/04/30 22:07

代码如下:
<Window x:Class="TextBoxBinding.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        
        <TextBox x:Name="Input" Text="Please input text!" Grid.Row="0"></TextBox>
    
        <TextBlock x:Name="Output1" Text="{Binding ElementName=Input, Path=Text}" Grid.Row="1"></TextBlock>

    </Grid>
</Window>

红色代码实现了TextBlock的Text属性与TextBox的Text属性的绑定. ElementName属性:指定当前XAML文档中任何以(x:Name)名称为ElementName值的对象为数据源。Path属性:与“数据源指定标记”一起使用,获取数据源中的成员(属性)。如果数据源对象继承了ICustomeTypeDescriptor接口,将会从接口中获取属性值,否则使用类反射获取。

也可以如下写法:
 <TextBlock x:Name="Output1" Grid.Row="1">
            <TextBlock.Text>
                <Binding ElementName="Input" Path="Text"></Binding>
            </TextBlock.Text>
  </TextBlock>

原创粉丝点击