如何TemplateBind到BorderThickness.Top(或Bottom或Left或Right)?

发布于 2024-08-04 07:47:49 字数 509 浏览 13 评论 0原文

我想知道是否可以将像 BorderThickness.Top 这样的结构元素绑定到 TemplatedParent 的相应属性。我已经尝试过,

<Border Margin="0" Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}">
    <Border.BorderThickness>
        <Thickness Left="0" Right="0" Top="{TemplateBinding BorderThickness.Top}" Bottom="{TemplateBinding BorderThickness.Bottom}"/>
    </Border.BorderThickness>
</Border>

我想这样做的原因是我希望 Left 和 Right 为 0,并且只绑定 Top 和 Bottom。

提前致谢。

I wonder if it is possible to bind a structure element like BorderThickness.Top to TemplatedParent's corresponding property. I have tried

<Border Margin="0" Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}">
    <Border.BorderThickness>
        <Thickness Left="0" Right="0" Top="{TemplateBinding BorderThickness.Top}" Bottom="{TemplateBinding BorderThickness.Bottom}"/>
    </Border.BorderThickness>
</Border>

The reason i want to do this is i want Left and Right to be 0 and only Top and Bottom be bound.

Thanks in advance.

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

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

发布评论

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

评论(2

陪你到最终 2024-08-11 07:47:49

这是不可能的,因为厚度是值类型 - 您只能在依赖项对象的依赖项属性上创建绑定。

您可以做的是像平常一样绑定 BorderThickness:

<Border Margin="0" 
        Padding="{TemplateBinding Padding}" 
        BorderBrush="{TemplateBinding BorderBrush}"
        BorderThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessConverter}}" />

然后使用转换器返回适当修改的 Thickness:

object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
{
    var thickness = (Thickness) value;
    return new Thickness( 0.0, thickness.Top, 0.0, thickness.Bottom );
}

您甚至可以使用 ConverterParameter 来指定要清除 Thickness 的哪些部分。

This is not possible because Thickness is a value-type - you can only create bindings on dependency properties of dependency objects.

What you could do is binding BorderThickness as normal:

<Border Margin="0" 
        Padding="{TemplateBinding Padding}" 
        BorderBrush="{TemplateBinding BorderBrush}"
        BorderThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessConverter}}" />

then use a converter to return an appropriately modified Thickness:

object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
{
    var thickness = (Thickness) value;
    return new Thickness( 0.0, thickness.Top, 0.0, thickness.Bottom );
}

You could even use ConverterParameter to specify which parts of the Thickness to clear.

失去的东西太少 2024-08-11 07:47:49

使用转换器的解决方案是正确的。

如果您只对一个值感兴趣,则可以直接在 XAML 中执行此操作,无需使用转换器{TemplateBinding …} 只是 {BindingrelativeSource={RelativeSource TemplatedParent} …} 的语法糖,功能有限。

例如一些自定义边框:

<Button BorderBrush="Purple"
        BorderThickness="1 2 3 4"
        Content="This is a button!">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <DockPanel>
                <Rectangle DockPanel.Dock="Left"
                           Width="{Binding BorderThickness.Left, RelativeSource={RelativeSource TemplatedParent}}"
                           Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
                <Rectangle DockPanel.Dock="Top"
                           Height="{Binding BorderThickness.Top, RelativeSource={RelativeSource TemplatedParent}}"
                           Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
                <Rectangle DockPanel.Dock="Right"
                           Width="{Binding BorderThickness.Right, RelativeSource={RelativeSource TemplatedParent}}"
                           Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
                <Rectangle DockPanel.Dock="Bottom"
                           Height="{Binding BorderThickness.Bottom, RelativeSource={RelativeSource TemplatedParent}}"
                           Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
                <ContentPresenter />
            </DockPanel>
        </ControlTemplate>
    </Button.Template>
</Button>

The solution with a converter is the right one.

In the case where you are interested only in one value, you can do this directly in the XAML without a converter. {TemplateBinding …} is only a syntactic sugar for {Binding RelativeSource={RelativeSource TemplatedParent} …} with a limited functionality.

For example some custom border:

<Button BorderBrush="Purple"
        BorderThickness="1 2 3 4"
        Content="This is a button!">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <DockPanel>
                <Rectangle DockPanel.Dock="Left"
                           Width="{Binding BorderThickness.Left, RelativeSource={RelativeSource TemplatedParent}}"
                           Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
                <Rectangle DockPanel.Dock="Top"
                           Height="{Binding BorderThickness.Top, RelativeSource={RelativeSource TemplatedParent}}"
                           Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
                <Rectangle DockPanel.Dock="Right"
                           Width="{Binding BorderThickness.Right, RelativeSource={RelativeSource TemplatedParent}}"
                           Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
                <Rectangle DockPanel.Dock="Bottom"
                           Height="{Binding BorderThickness.Bottom, RelativeSource={RelativeSource TemplatedParent}}"
                           Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
                <ContentPresenter />
            </DockPanel>
        </ControlTemplate>
    </Button.Template>
</Button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文