【发布时间】:2011-09-28 08:38:15
【问题描述】:
是否可以完全在 XAML 中将某些内容绑定到数据模板中的控件属性?以下代码是我遇到的问题的简化版本。我希望 TextBlock (displayName) 的文本随着用户在位于 DataTemplate 中的 TextBox 中键入而更新。
<Window x:Class="WpfApplication4.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication4="clr-namespace:WpfApplication4"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate DataType="{x:Type WpfApplication4:Foo}">
<TextBox Text="{Binding Path=Name}" />
</DataTemplate>
<WpfApplication4:Foo x:Key="testObject" Name="This is a test" />
</Window.Resources>
<StackPanel>
<TextBlock x:Name="displayName" Margin="5" />
<ContentControl x:Name="contentControl" Margin="5" Content="{StaticResource testObject}" />
</StackPanel>
【问题讨论】:
标签: wpf data-binding