在 ControlTemplate 中的样式中使用 AttachedProperty

发布于 2024-09-03 06:39:39 字数 2273 浏览 4 评论 0原文

这是我的简单应用程序:

<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"

提前致谢!

Here is my simple application:

<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>

Here is my 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}" this of course doesn't work. so how can I get value from here app:Extras.CornerRadius="10"

thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

自由如风 2024-09-10 06:39:39

使用 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}}"
        >    

Use a TemplateBinding rather than a Binding :

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

OK, try that then :

<Border BorderBrush="Blue"
        BorderThickness="3"
        Background="Black"
        CornerRadius="{Binding (app:Extras.CornerRadius), RelativeSource={x:Static RelativeSource.TemplatedParent}}"
        >    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文