首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TemplateBinding的奇怪行为

TemplateBinding的奇怪行为
EN

Stack Overflow用户
提问于 2022-05-25 21:23:00
回答 1查看 24关注 0票数 1

我正在尝试创建一个自定义控件。在将cornerRadius属性设置为“绑定”之后,它只对默认半径"8“起作用。以下是财产的定义:

代码语言:javascript
复制
public CornerRadius cornerRadius
{
    get { return (CornerRadius)GetValue(cornerRadiusProperty); }
    set { SetValue(cornerRadiusProperty, value); }
}

public static readonly DependencyProperty cornerRadiusProperty =
    DependencyProperty.Register("cornerRadius", typeof(CornerRadius), typeof(expandMenuA), new PropertyMetadata(new CornerRadius(8)));

在主边框中,一切正常工作,但第二次,它只对默认的"8“起作用,当我在不同的项目中更改一个值时,这个值引用了这个自定义控件,什么都不会发生,并且它仍然是默认的:

代码语言:javascript
复制
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:expandMenuA">
    <Style TargetType="{x:Type local:expandMenuA}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:expandMenuA}">
                    <Border CornerRadius="{TemplateBinding cornerRadius}" x:Name="mainBorder" Background="#232323">
                        <StackPanel>
                            <Button Height="{TemplateBinding expanderHeight}" Background="#2d2d2d">
                                <Button.Template>
                                    <ControlTemplate TargetType="Button">
                                        <Border CornerRadius="{TemplateBinding local:expandMenuA.cornerRadius}" Background="{TemplateBinding Background}">
                                            <ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center"/>
                                        </Border>
                                    </ControlTemplate>
                                </Button.Template>
                                <Button.Content>
                                    <TextBlock Text="{TemplateBinding menuTitle}" Foreground="White" Margin="10, 0, 0, 0" Background="{TemplateBinding BorderBrush}"/>
                                </Button.Content>
                            </Button>
                            <ContentPresenter/>
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
</ResourceDictionary>
EN

回答 1

Stack Overflow用户

发布于 2022-05-25 21:28:45

按钮中的TemplateBinding将查找Button的属性,该属性将解析为默认的DP值。与RelativeSource一起使用绑定:

代码语言:javascript
复制
<ControlTemplate TargetType="Button">
    <Border CornerRadius="{Binding Path=cornerRadius, RelativeSource={RelativeSource AncestorType={x:Type local:expandMenuA}}}" Background="{TemplateBinding Background}">
        <ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center"/>
    </Border>
</ControlTemplate>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72384330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档