Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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
C# ContentPresenter的绑定内容_C#_.net_Wpf_Xaml_Visual Studio 2012 - Fatal编程技术网

C# ContentPresenter的绑定内容

C# ContentPresenter的绑定内容,c#,.net,wpf,xaml,visual-studio-2012,C#,.net,Wpf,Xaml,Visual Studio 2012,我正在制作一个自定义标签 Border CornerRadius="10" Background="#428bca" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"> <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" > <TextBlock.Fore

我正在制作一个
自定义标签

Border CornerRadius="10"  Background="#428bca" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}">
        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" >
            <TextBlock.Foreground>
                White
            </TextBlock.Foreground>
        </ContentPresenter>
    </Border>
Border CornerRadius=“10”Background=“#428bca”Height=“{TemplateBinding Height}”Width=“{TemplateBinding Width}”>
白色

我想绑定
ContentPresenter
的内容,这样,当我更改内容长度时,边框的
大小(宽度和高度)将自动调整,这样我就不必手动更改它。

下面的标签应该满足您的要求。请注意,如果标签是网格的子项,则必须将“对齐特性”设置为默认的“拉伸”
“拉伸”,否则面板将调整标签的大小(例如,调整标签所占网格单元的大小)


为了对所有标签重复使用此ControlTemplate,您可以将其设置为默认标签样式:

<Style TargetType="Label">
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="VerticalAlignment" Value="Top"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Label">
                <Border CornerRadius="10" Background="#428bca"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <ContentPresenter Margin="{TemplateBinding Padding}"
                        Content="{TemplateBinding Content}"
                        ContentTemplate="{TemplateBinding ContentTemplate}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


只需删除宽度和高度绑定。不,这不起作用!!然后,你可以考虑增加一些你想要达到的细节。您正在重新设计标签的样式吗?您的自定义标签与现有标签有何不同?是的,我。。。基本上,我的标签有圆角,并且有一些固定的背景色。@Ravi-if
TemplateBinding
在上述@Clemens的优秀示例中不起作用,请尝试将其更改为
RelativeSource
binding@Jasper我不明白你的评论。没有理由认为TemplateBinding不起作用。当然,可以使用TemplatedParent作为RelativeSource(这是您应该提到的最低要求),用常规绑定替换TemplateBinding,但是没有必要这样做。
<Style TargetType="Label">
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="VerticalAlignment" Value="Top"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Label">
                <Border CornerRadius="10" Background="#428bca"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <ContentPresenter Margin="{TemplateBinding Padding}"
                        Content="{TemplateBinding Content}"
                        ContentTemplate="{TemplateBinding ContentTemplate}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>