【问题标题】:Binding to property on root element in DataTemplate through a ContentControl通过 ContentControl 绑定到 DataTemplate 中根元素的属性
【发布时间】:2010-10-07 20:57:50
【问题描述】:

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

我想在 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 属性中获取单词“作者”?

【问题讨论】:

    标签: wpf data-binding datatemplate contentcontrol


    【解决方案1】:

    如何通过您的数据对象进行路由:

    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>
    

    【讨论】:

    • 谢谢。我想在代码隐藏中将 TypeName 设置为 AutomationProperties.Name 的内容?
    • 一定有某个地方设置了 Person.TypeName ,是的。一般来说,我不喜欢 codebhind (goo.gl/KyTW)。但如果你使用它,是的,这可能就是这个地方。
    • 当然你需要对属性使用 INotifyPropertyChanged 模式。你有吗?
    【解决方案2】:

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

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

    但是,我仍然认为不使用 ViewModel 应该可以做到这一点,我希望能够在我的数据绑定技能提高后重新审视这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      • 2021-05-06
      • 2014-11-04
      • 1970-01-01
      • 2018-09-30
      相关资源
      最近更新 更多