【发布时间】:2013-03-13 15:11:50
【问题描述】:
我有以下简化示例:
<Window x:Class="TemplateBinding.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">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/TemplateBinding;component/PersonTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ContentControl ContentTemplate="{StaticResource PersonTemplate}" />
</Grid>
</Window>
与:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="PersonTemplate">
<Border Width="100" Height="100" Background="RosyBrown">
<TextBlock Text="{Binding Path=FirstName}" VerticalAlignment="Center" TextAlignment="Center"/>
</Border>
</DataTemplate>
</ResourceDictionary>
在单独的 ResourceDictionary 文件中作为我的 DataTemplate。
我在我的 MainWindow 的构造函数中设置了我的 DataContext 并通过仅显示这样的名字来验证它:<ContentControl Grid.Row="1" Content="{Binding FirstName}"/>。
在另一种情况下,我将 DataTemplate 与 ListBox 一起使用,我在 DataTemplate 中以完全相同的方式进行绑定,并且它可以正常工作。
我知道除了绑定之外 DataTemplate 正在工作,因为它正确显示了大小和背景颜色。
我做错了什么?我的 DataTemplate 中的 Binding 看起来如何?
【问题讨论】:
标签: wpf data-binding datatemplate resourcedictionary contentcontrol