通过 ContentControl 绑定到 DataTemplate 中根元素的属性
在我的用户界面中,我有时想将标题放在用户控件之上。
我想在 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>
实际问题是:如何在“Position”标签的内容属性中使用数据绑定从 DataTemplate 中边框的 AutomationProperties.Name 属性中获取单词“Author”?
In my user interface I sometimes want to put titles above usercontrols.
I want to declare these titles in XAML for future localizability, so I want to keep them out of the datacontexts.
Can databinding fetch them from a property set on the root node of the usercontrol?
I have boiled the problem down to the following code example:
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; }
}
}
And:
<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>
And the practical problem is: how can I use databinding in the content property of the "Position" Label to fetch the word "Author" from the AutomationProperties.Name property of the Border in the DataTemplate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过您的数据对象进行路由怎么样:
以及:
How about going the route over your data object:
And:
到目前为止的解决方案是将 TypeName 的字符串属性添加到视图模型中,并用代码隐藏中的 AutomationProperties.Name 的内容填充它。并使用以下绑定:
但是,我仍然认为应该可以在不使用 ViewModel 的情况下做到这一点,我希望当我的数据绑定技能得到提高时能够重新审视这个问题。
The solution so far is to add a string property for TypeName to the viewmodel and fill this with the contents of the AutomationProperties.Name in code-behind. And use the following binding:
However, I still think it should be possible to do this without using the ViewModel, I hope to be able to revisit this issue when my databinding skills have improved.