【问题标题】:Propagate an attached dependency property value to a template将附加的依赖属性值传播到模板
【发布时间】:2013-06-16 20:10:45
【问题描述】:

可以将附加的依赖属性值传播到模板化的孩子吗?

示例:尝试将 TagCloudService.* 属性传播到 ItemTemplate

<ItemsControl x:Name="myItemsControl"
                pages:TagCloudService.MaximumFontSize="20"
                pages:TagCloudService.MaximumFontWeight="800"
                pages:TagCloudService.MinimumFontSize="10"
                pages:TagCloudService.MinimumFontWeight="400"
                pages:TagCloudService.NumberOfSizes="5"
                pages:TagCloudService.TagFrequency="{Binding Hotttnesss}"
                pages:TagCloudService.TagWeight="{Binding Weight}"
                Template="{StaticResource TagCloudItemsControlTemplate}">
    <ItemsControl.ItemTemplate>
        <StaticResource ResourceKey="TermTagCloudTemplate" />
    </ItemsControl.ItemTemplate>
</ItemsControl>

这里是项目模板,其中定义了一些默认值但应该被覆盖:

<DataTemplate x:Key="TermTagCloudTemplate" DataType="api:Term">
        <TextBlock Foreground="DodgerBlue"
                    Padding="10"
                    Style="{StaticResource TagCloudTextBlockStyle}"
                    Text="{Binding Name}"
                    pages:TagCloudService.MaximumFontSize="30"
                    pages:TagCloudService.MaximumFontWeight="800"
                    pages:TagCloudService.MinimumFontSize="20"
                    pages:TagCloudService.MinimumFontWeight="400"
                    pages:TagCloudService.NumberOfSizes="5"
                    pages:TagCloudService.TagFrequency="{Binding Frequency}"
                    pages:TagCloudService.TagWeight="{Binding Weight}">
        </TextBlock>
</DataTemplate>

我尝试设置 FrameworkPropertyMetadataOptions.OverridesInheritanceBehavior 而不是 .Inherits,尝试在模板中注释默认值但属性未传播。

这可能吗,还是我应该为该控件创建另一个 ItemTemplate ?

【问题讨论】:

标签: wpf attached-properties


【解决方案1】:

其实很简单。

使用Inherits-option 声明附加属性:

public static string GetFoo(DependencyObject obj)
{
    return (string)obj.GetValue(FooProperty);
}

public static void SetFoo(DependencyObject obj, string value)
{
    obj.SetValue(FooProperty, value);
}

public static readonly DependencyProperty FooProperty =
    DependencyProperty.RegisterAttached(
        "Foo", typeof(string), typeof(MainWindow),
        new FrameworkPropertyMetadata(
               "default",
               // this bit is important:
               FrameworkPropertyMetadataOptions.Inherits));

测试 XAML:

<Grid src:MainWindow.Foo="non-default">
    <TextBlock Text="{Binding (src:MainWindow.Foo),
                              RelativeSource={RelativeSource Self}}"/>
</Grid>

显示“非默认”。

这也适用于模板边界,完整示例如下。

<Window x:Class="DPInheritance.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:DPInheritance"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="Test">
            <TextBlock Text="{Binding (src:MainWindow.Foo),
                                      RelativeSource={RelativeSource Self}}"/>
        </DataTemplate>
    </Window.Resources>
    <Grid src:MainWindow.Foo="non-default">
        <ItemsControl ItemTemplate="{StaticResource Test}">
            <sys:Int32>0</sys:Int32>
            <sys:Int32>1</sys:Int32>
            <sys:Int32>2</sys:Int32>
        </ItemsControl>
    </Grid>
</Window>

【讨论】:

  • 不幸的是,RelativeSource Self 不起作用,但 FindAncestor 起作用。
  • @Aybe:奇怪,它在我的机器上运行成功。你真的更正了附加的属性定义吗?
  • @Aybe:好吧。你能在你的机器上试试我的代码示例吗? (这里是代码隐藏:pastebin.com/73iRhPJK
  • 我刚刚尝试过,这很奇怪:它仅在运行时有效,不适用于引发 XamlParseException: Prefix 'src' does not map to a namespace.
  • 感谢您的帮助,我想我会坚持创建另一个模板,因为我无法从 ItemsControl 中获取最后两个附加属性绑定,不知道如何作为这些成员进行操作无法解决。我仍然会将您的答案标记为答案,因为与此同时,如果您对如何从 ItemsControl 绑定这两个有任何想法,欢迎您。
猜你喜欢
  • 2020-12-22
  • 1970-01-01
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
  • 2010-11-08
  • 2021-01-29
  • 2015-11-25
  • 1970-01-01
相关资源
最近更新 更多