在我的用户界面中,我有时想将标题放在用户控件之上。
我想在 XAML 中声明这些标题以供将来本地化,所以我想将它们排除在数据上下文之外。
数据绑定可以从用户控件的根节点上的属性集中获取它们吗?
我将问题归结为以下代码示例:
using System.Windows;
namespace WpfApplication12
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Person = new Author { Name = "Guge" };
this.DataContext = this;
}
public object Person { get; set; }
}
public class Author
{
public string Name { get; set; }
}
}
和:
<Window x:Class="WpfApplication12.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication12"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Author}">
<Border AutomationProperties.Name="Author" BorderThickness="1" BorderBrush="Black">
<Label Content="{Binding Name}"/>
</Border>
</DataTemplate>
</Window.Resources>
<StackPanel>
<Label x:Name="Position" Content="Author"/>
<ContentControl x:Name="presentation" Content="{Binding Person}"/>
</StackPanel>
实际问题是:如何在“位置”标签的内容属性中使用数据绑定从 DataTemplate 中边框的 AutomationProperties.Name 属性中获取单词“作者”?