您的位置:首页 > 其它

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

2008-11-17 22:09 253 查看
代码如下:
<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>

本文出自 “木子纵横” 博客,请务必保留此出处http://muzizongheng.blog.51cto.com/856912/1333197
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: