带圆角的标签和扩展器控件

问题描述:

我尝试为标签和扩展器控件制作一个带有圆角的样式.

Hi I try make a style with rounded corners for label and expander control.

标签样式:

        <Style x:Key="InfoLabelStyle" TargetType="{x:Type Label}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Label}">
                        <Border Name="Border" Background="#BFE3FE" BorderBrush="#BFE3FE" BorderThickness="1" CornerRadius="7" Padding="3">
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="FontWeight" Value="Normal"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="BorderBrush" Value="#BFE3FE"/>
            <Setter Property="Background" Value="#BFE3FE"/>
            <Setter Property="Margin" Value="2,4,0,1" />
            <Setter Property="Padding" Value="4,0,0,0" />
        </Style>

我在视图中对此样式使用了多重绑定:

I use multibinding on this style in view:

                 <Label Style="{StaticResource InfoLabelStyle}">
                        <Label.Content>
                            <MultiBinding StringFormat="{}{0}, {1} rokov">
                                <Binding Path="Oponent.Info.Sex" Converter="{StaticResource sexConverter}"/>
                                <Binding Path= "Oponent.Info.Age"/>
                            </MultiBinding>
                        </Label.Content>
                      </Label>

但是如果我运行应用程序,则此标签的内容为空,绑定很好,我可以在textBox控件上尝试并工作.

But if I run app, content of this label is empty, binding is good, I try it on textBox control and work.

第二个问题是,我想在扩展器控件上也弄上个圆角.

Second problem is, I would like have also rounded corners on expander control.

我尝试使用与标签样式相同的方法:

I try same way as in label style:

        <Style x:Key="InfoExpanderStyle" TargetType="{x:Type Expander}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Expander}">
                        <Border Name="Border" Background="#BFE3FE" BorderBrush="#BFE3FE" BorderThickness="1" CornerRadius="7" Padding="3">
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

扩展器控件上的应用样式:

Applu style on Expander control:

     <Expander Name="InfoExapnder" 
                      Header="{Binding Path=Oponent.Info.Nick, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                      Style="{StaticResource InfoExpanderStyle}"
                      IsExpanded="True"
                      FontSize="18"
                      FontWeight="Normal"
                      Background="#ECEBEB" 
                      Margin="3,0,3,0"
                      Grid.Row="0">
                <Grid>
</Grid>

但是结果是一样的,控件的内容为空.

But result is same, empty content of the control.

我做错了什么?

标签为空,因为您已经对其重新模板化,但是未指定内容的放置位置.您想要类似的东西:

The label is empty because you've re-templated it but not specified where the content should be placed. You want something like:

                <ControlTemplate TargetType="{x:Type Label}">
                    <Border Name="Border" Background="#BFE3FE" BorderBrush="#BFE3FE" BorderThickness="1" CornerRadius="7" Padding="3">
                        <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                    </Border>
                </ControlTemplate>