ControlTemplate - 边框设置CornerRadius [英] ControlTemplate - Border set CornerRadius

查看:51
本文介绍了ControlTemplate - 边框设置CornerRadius的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述









我的内容控件内有边框。我必须使用此控件在切换按钮控件(仅左角,中间和右角)中制作不同的角半径。要做到这一点,我做了3个不同的控制模板,因为边框
的CornerRadius不接受模板绑定。



可以制作一个独特的controltemplate并将角半径作为TemplateBinding传递? 
$


谢谢



Hi,

I've a content control with a Border inside. I have to use this control to make different corner radius in a toggle button control (only left corner, center and right corner). To do this I've make 3 differente controltemplate because the CornerRadius of border don't accept a TemplateBinding for that.

It's possible to make an unique controltemplate and passing the corner radius as TemplateBinding? 

Thanks

Mrutyunjaya

Mrutyunjaya

推荐答案

您好Mrutyunjaya M,

Hi Mrutyunjaya M,

有两种方法可以做到这一点,一种是使用基础,另一种是使用附加属性。

There are two way to do this, one is using Basedon, another is using attached property.

关于basedon,请看一下:

About basedon, please take a look:

 <Window.Resources>
      

        <Style x:Key="TabButton" TargetType="Button">
            <Setter Property="TextBlock.TextAlignment" Value="Center" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding Background}"
                            BorderThickness="0,1,1,0"
                            CornerRadius="{TemplateBinding Border.CornerRadius}">
                            <ContentPresenter
                                x:Name="contentPresenter"
                                Margin="{TemplateBinding Padding}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                Content="{TemplateBinding Content}"
                                ContentTemplate="{TemplateBinding ContentTemplate}" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style
            x:Key="TabButtonFirst"
            BasedOn="{StaticResource TabButton}"
            TargetType="Button">
            <Setter Property="Border.CornerRadius" Value="15,0,0,0" />
        </Style>

        <Style
            x:Key="TabButtonLast"
            BasedOn="{StaticResource TabButton}"
            TargetType="Button">
            <Setter Property="Border.CornerRadius" Value="0,0,0,15" />
        </Style>
    </Window.Resources>
    <StackPanel>
       
        <Button
            Width="200"
            Height="30"
            Content="test" Style="{StaticResource TabButtonFirst}"/>
        <Button
            Width="200"
            Height="30"
            Content="test" Style="{StaticResource TabButtonLast}"/>
    </StackPanel>

关于附属物,请查看以下主题:

About Attached property, please take a look the following thread:

https://stackoverflow.com/questions/31842782/wpf-how- to-binding-cornerradius-of-border-inside-button-template

最好的问候,

Cherry


这篇关于ControlTemplate - 边框设置CornerRadius的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆