通过 ContentControl 绑定到 DataTemplate 中根元素的属性

发布于 2024-09-26 17:45:42 字数 1585 浏览 1 评论 0原文

在我的用户界面中,我有时想将标题放在用户控件之上。

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2024-10-03 17:45:42

通过您的数据对象进行路由怎么样:

public class Author
{
    public string Name { get; set; }
    public string TypeName { get; set; } // might be better in base class Person
}

以及:

<Window.Resources>
    <DataTemplate DataType="{x:Type local:Author}">
        <Border AutomationProperties.Name="{Binding TypeName}" 
                BorderThickness="1" BorderBrush="Black">
            <Label Content="{Binding Name}"/>
        </Border>
    </DataTemplate>
</Window.Resources>
<StackPanel>
    <Label x:Name="Position" Content="{Binding ElementName=presentation, Path=DataContext.TypeName}"/>
    <ContentControl x:Name="presentation" Content="{Binding Person}"/>
</StackPanel>

How about going the route over your data object:

public class Author
{
    public string Name { get; set; }
    public string TypeName { get; set; } // might be better in base class Person
}

And:

<Window.Resources>
    <DataTemplate DataType="{x:Type local:Author}">
        <Border AutomationProperties.Name="{Binding TypeName}" 
                BorderThickness="1" BorderBrush="Black">
            <Label Content="{Binding Name}"/>
        </Border>
    </DataTemplate>
</Window.Resources>
<StackPanel>
    <Label x:Name="Position" Content="{Binding ElementName=presentation, Path=DataContext.TypeName}"/>
    <ContentControl x:Name="presentation" Content="{Binding Person}"/>
</StackPanel>
百变从容 2024-10-03 17:45:42

到目前为止的解决方案是将 TypeName 的字符串属性添加到视图模型中,并用代码隐藏中的 AutomationProperties.Name 的内容填充它。并使用以下绑定:

<StackPanel>
    <Label x:Name="Position" Content="{Binding Person.TypeName}"/>
    <ContentControl x:Name="presentation" Content="{Binding Person}"/>
</StackPanel>

但是,我仍然认为应该可以在不使用 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:

<StackPanel>
    <Label x:Name="Position" Content="{Binding Person.TypeName}"/>
    <ContentControl x:Name="presentation" Content="{Binding Person}"/>
</StackPanel>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文