【问题标题】:Use AttachedProperty in Style in ControlTemplate在 ControlTemplate 的样式中使用 AttachedProperty
【发布时间】:2010-05-24 16:44:06
【问题描述】:

这是我的简单应用:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:app="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style x:Key="Test">
            <Setter Property="Button.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border BorderBrush="Blue"
                                BorderThickness="3"
                                Background="Black"
                                CornerRadius="{Binding app:Extras.CornerRadius}"
                                >                            
                        </Border>
                        </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Height="23" 
                HorizontalAlignment="Left"
                Margin="29,26,0,0" 
                Name="button1" 
                VerticalAlignment="Top" Width="75"
                app:Extras.CornerRadius="10"
                Style="{StaticResource Test}"
                >Button</Button>
    </Grid>
</Window>

这是我的 AttachedProperty:

namespace WpfApplication1
{
    public class Extras
    {
        public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached(
          "CornerRadius",
          typeof(double),
          typeof(Button),
          new FrameworkPropertyMetadata(1.0d, FrameworkPropertyMetadataOptions.AffectsRender)
        );

        public static void SetCornerRadius(UIElement element, double value)
        {
            element.SetValue(CornerRadiusProperty, value);
        }
        public static double GetCornerRadius(UIElement element)
        {
            return (double)element.GetValue(CornerRadiusProperty);
        }

    }
}

CornerRadius="{Binding app:Extras.CornerRadius}" 这当然行不通。那么我怎样才能从这里获得价值app:Extras.CornerRadius="10"

提前致谢!

【问题讨论】:

    标签: .net wpf xaml dependency-properties attached-properties


    【解决方案1】:

    使用TemplateBinding 而不是Binding

                        <Border BorderBrush="Blue"
                                BorderThickness="3"
                                Background="Black"
                                CornerRadius="{TemplateBinding app:Extras.CornerRadius}"
                                >            
    

    好的,那就试试吧:

    <Border BorderBrush="Blue"
            BorderThickness="3"
            Background="Black"
            CornerRadius="{Binding (app:Extras.CornerRadius), RelativeSource={x:Static RelativeSource.TemplatedParent}}"
            >    
    

    【讨论】:

    • 在设计器中给出错误:“无法在类型 'Button' 上找到静态成员 'CornerRadiusProperty'。” ,在运行时“无法将属性‘CornerRadius’中的值转换为‘System.Windows.TemplateBindingExtension’类型的对象。标记文件中的对象‘System.Windows.Controls.ControlTemplate’出错”
    • System.Windows.Data 错误:39:BindingExpression 路径错误:在“对象”“按钮”(名称=“button1”)上找不到“(app:Extras.CornerRadius)”属性。 BindingExpression:Path=(app:Extras.CornerRadius);数据项='按钮'(名称='button1');目标元素是 'Border' (Name='');目标属性是“CornerRadius”(输入“CornerRadius”)
    猜你喜欢
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    相关资源
    最近更新 更多