<Window x:Class="Demo1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<!--资源-->
<StackPanel.Resources>
<sys:String x:Key="myString">
菩提本无树,明镜亦非台。
本来无一物,何处惹尘埃。
</sys:String>
</StackPanel.Resources>
<!--
注意大小写,Slider默认大小为 10
TextBox Text属性中用到了标记扩展
-->
<TextBox Text="{Binding ElementName=slider1,Path=Value,Mode=OneWay}" Margin="5"/>
<TextBox x:Name="txtValue" Margin="5"/>
<Slider x:Name="slider1" Margin=" 5" Maximum="100"/> <!--静态资源-->
<TextBlock x:Name="txttest" Margin="5" TextWrapping="Wrap" Text="{Binding Source={StaticResource ResourceKey=myString}}"/> </StackPanel> </Grid> <x:Code>
<!--WPF中实现了代码后置操作
x:code可以将 后置的代码写入其中--> <![CDATA[ ]]>
</x:Code>
</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data; namespace Demo1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); this.txtValue.SetBinding(TextBox.TextProperty, new Binding("Value") { ElementName="slider1"});
}
}
}
<Application x:Class="Demo1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources> </Application.Resources>
</Application>
运行效果如下: