Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何将模板绑定到BorderThickness.Top(或底部、左侧或右侧)?_Wpf_Binding_Templatebinding_Thickness - Fatal编程技术网

Wpf 如何将模板绑定到BorderThickness.Top(或底部、左侧或右侧)?

Wpf 如何将模板绑定到BorderThickness.Top(或底部、左侧或右侧)?,wpf,binding,templatebinding,thickness,Wpf,Binding,Templatebinding,Thickness,我想知道是否可以将BorderThickness.Top这样的结构元素绑定到TemplatedParent的相应属性。我试过了 <Border Margin="0" Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}"> <Border.BorderThickness> <Thickness Left="0" Right="0" Top=

我想知道是否可以将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


提前感谢。

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

您可以做的是像正常情况一样绑定边界厚度:

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

您甚至可以使用ConverterParameter指定要清除的厚度部分。

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

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

例如,某些自定义边框:



ahaa。。。非常感谢您快速准确的回复。。。我最近了解了依赖属性。。。我怎么能忘记呢?!这不起作用,因为
Thickness.Top
不是DependencyProperty。它会导致XAMLParseException。
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 );
}
<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>