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

发布于 2024-10-12 16:24:29 字数 3060 浏览 1 评论 0原文

您好,我尝试制作一种带有圆角的样式,用于标签和扩展器控制。

标签样式:

        <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>

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

                 <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>

但是如果我运行应用程序,该标签的内容为空,绑定良好,我在文本框控件上尝试并工作。

第二个问题是,我希望扩展器控制也有圆角。

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

        <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>

Expander 控件上的 Applu 样式:

     <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>

但结果是相同的,控件的内容为空。

我做什么坏事了?

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

Label style:

        <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>

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.

What I do bad?

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

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

发布评论

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

评论(1

唯憾梦倾城 2024-10-19 16:24:29

该标签为空,因为您已对其进行了重新模板化,但未指定内容应放置的位置。你想要这样的东西:

                <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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文