Silverlight 字段集控制

发布于 2024-09-30 10:31:03 字数 70 浏览 0 评论 0原文

我一直缺少 Silverlight 中 HTML 的经典 Fieldset,并且在网络上找不到任何解决方案。我该如何构建一个?

I've been missing the classic Fieldset of HTML in Silverlight and I couldn't find any solutions on the web. How do I go about building one?

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

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

发布评论

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

评论(2

流心雨 2024-10-07 10:31:03

我想我会建造一个。

这可能不是解决问题的最佳方法,但它有效,我只是想分享它,因为感觉其他人可能正在寻找同样的东西。

不过,解决方案很简单,您可以设置图例的字体大小、前景和标题。

标记:

<Controls:Fieldset BorderBrush="#FFcccccc" Legend="LegendHeader" LegendFontSize="14" LegendForeground="Green">
   <Button Content="Button" />
</Controls:Fieldset>

控件样式:

<Style TargetType="Controls:Fieldset">
    <Setter Property="Padding" Value="10"/>
    <Setter Property="Margin" Value="10"/>
    <Setter Property="BorderBrush" Value="#FFcccccc"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="LegendFontSize" Value="14"/>
    <Setter Property="LegendForeground" Value="Black"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Controls:Fieldset">
                <Grid x:Name="LayoutRoot" Margin="{TemplateBinding Margin}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="5"/>
                        <ColumnDefinition Width="20"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="5"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="5"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <Border BorderThickness="1,1,0,0" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="5,0,0,0"/>
                    <Border Grid.Column="1" BorderThickness="0,1,0,0" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"/>
                    <Border Grid.Column="3" BorderThickness="0,1,0,0" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"/>
                    <Border Grid.Column="4" BorderThickness="0,1,1,0" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="0,5,0,0"/>
                    <Border Grid.ColumnSpan="5" Grid.Row="1" BorderThickness="1,0,1,1" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="0,0,5,5"/>
                    <Border Background="{TemplateBinding Background}" Margin="0,1,0,0" Grid.Column="2"/>
                    <Grid  Grid.Column="2" Margin="10,-30,10,-30">
                        <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="{TemplateBinding LegendFontSize}" Foreground="{TemplateBinding LegendForeground}" Text="{TemplateBinding Legend}"/>
                    </Grid>
                    <Border Background="{TemplateBinding Background}" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3"/>
                    <ContentPresenter
                        Grid.Column="1" 
                        Grid.ColumnSpan="3" 
                        Grid.Row="1"
                        Margin="{TemplateBinding Padding}" 
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"
                        Content="{TemplateBinding Content}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

和类:

Public Class Fieldset
    Inherits ContentControl

    Public Sub New()
    End Sub

    Public Shared ReadOnly LegendProperty As DependencyProperty = DependencyProperty.
        Register("Legend", GetType(String), GetType(Fieldset), New PropertyMetadata(AddressOf OnLegendChanged))

    Private Shared Sub OnLegendChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim fieldset = TryCast(d, Fieldset)
        fieldset.Legend = e.NewValue.ToString()
    End Sub

    Public Property Legend As String
        Get
            Return Me.GetValue(LegendProperty).ToString()
        End Get
        Set(ByVal value As String)
            MyBase.SetValue(LegendProperty, value)
        End Set
    End Property

    Public Shared ReadOnly LegendFontSizeProperty As DependencyProperty = DependencyProperty.
        Register("LegendFontSize", GetType(Double), GetType(Fieldset), New PropertyMetadata(AddressOf OnLegendFontSizeChanged))

    Private Shared Sub OnLegendFontSizeChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim fieldset = TryCast(d, Fieldset)
        fieldset.LegendFontSize = CDbl(e.NewValue)
    End Sub

    Public Property LegendFontSize As Double
        Get
            Return CDbl(Me.GetValue(LegendFontSizeProperty))
        End Get
        Set(ByVal value As Double)
            MyBase.SetValue(LegendFontSizeProperty, value)
        End Set
    End Property

    Public Shared ReadOnly LegendForegroundProperty As DependencyProperty = DependencyProperty.
        Register("LegendForeground", GetType(SolidColorBrush), GetType(Fieldset), New PropertyMetadata(AddressOf OnLegendForegroundChanged))

    Private Shared Sub OnLegendForegroundChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim fieldset = TryCast(d, Fieldset)
        fieldset.LegendForeground = DirectCast(e.NewValue, SolidColorBrush)
    End Sub

    Public Property LegendForeground As SolidColorBrush
        Get
            Return DirectCast(Me.GetValue(LegendForegroundProperty), SolidColorBrush)
        End Get
        Set(ByVal value As SolidColorBrush)
            MyBase.SetValue(LegendForegroundProperty, value)
        End Set
    End Property
End Class

对于 VB.NET 代码,我深表歉意。

正如我所说,可能有很多更好的解决方案,但这里是。

I figured I'll build one.

It's probably not the best way to solve it but it works and I just thought I'd share it because it feels like others might be looking for the same thing.

Simple solution though, you can set FontSize, Foreground and title of the legend.

Markup:

<Controls:Fieldset BorderBrush="#FFcccccc" Legend="LegendHeader" LegendFontSize="14" LegendForeground="Green">
   <Button Content="Button" />
</Controls:Fieldset>

Control Style:

<Style TargetType="Controls:Fieldset">
    <Setter Property="Padding" Value="10"/>
    <Setter Property="Margin" Value="10"/>
    <Setter Property="BorderBrush" Value="#FFcccccc"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="LegendFontSize" Value="14"/>
    <Setter Property="LegendForeground" Value="Black"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Controls:Fieldset">
                <Grid x:Name="LayoutRoot" Margin="{TemplateBinding Margin}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="5"/>
                        <ColumnDefinition Width="20"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="5"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="5"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <Border BorderThickness="1,1,0,0" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="5,0,0,0"/>
                    <Border Grid.Column="1" BorderThickness="0,1,0,0" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"/>
                    <Border Grid.Column="3" BorderThickness="0,1,0,0" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"/>
                    <Border Grid.Column="4" BorderThickness="0,1,1,0" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="0,5,0,0"/>
                    <Border Grid.ColumnSpan="5" Grid.Row="1" BorderThickness="1,0,1,1" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="0,0,5,5"/>
                    <Border Background="{TemplateBinding Background}" Margin="0,1,0,0" Grid.Column="2"/>
                    <Grid  Grid.Column="2" Margin="10,-30,10,-30">
                        <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="{TemplateBinding LegendFontSize}" Foreground="{TemplateBinding LegendForeground}" Text="{TemplateBinding Legend}"/>
                    </Grid>
                    <Border Background="{TemplateBinding Background}" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3"/>
                    <ContentPresenter
                        Grid.Column="1" 
                        Grid.ColumnSpan="3" 
                        Grid.Row="1"
                        Margin="{TemplateBinding Padding}" 
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"
                        Content="{TemplateBinding Content}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And class:

Public Class Fieldset
    Inherits ContentControl

    Public Sub New()
    End Sub

    Public Shared ReadOnly LegendProperty As DependencyProperty = DependencyProperty.
        Register("Legend", GetType(String), GetType(Fieldset), New PropertyMetadata(AddressOf OnLegendChanged))

    Private Shared Sub OnLegendChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim fieldset = TryCast(d, Fieldset)
        fieldset.Legend = e.NewValue.ToString()
    End Sub

    Public Property Legend As String
        Get
            Return Me.GetValue(LegendProperty).ToString()
        End Get
        Set(ByVal value As String)
            MyBase.SetValue(LegendProperty, value)
        End Set
    End Property

    Public Shared ReadOnly LegendFontSizeProperty As DependencyProperty = DependencyProperty.
        Register("LegendFontSize", GetType(Double), GetType(Fieldset), New PropertyMetadata(AddressOf OnLegendFontSizeChanged))

    Private Shared Sub OnLegendFontSizeChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim fieldset = TryCast(d, Fieldset)
        fieldset.LegendFontSize = CDbl(e.NewValue)
    End Sub

    Public Property LegendFontSize As Double
        Get
            Return CDbl(Me.GetValue(LegendFontSizeProperty))
        End Get
        Set(ByVal value As Double)
            MyBase.SetValue(LegendFontSizeProperty, value)
        End Set
    End Property

    Public Shared ReadOnly LegendForegroundProperty As DependencyProperty = DependencyProperty.
        Register("LegendForeground", GetType(SolidColorBrush), GetType(Fieldset), New PropertyMetadata(AddressOf OnLegendForegroundChanged))

    Private Shared Sub OnLegendForegroundChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim fieldset = TryCast(d, Fieldset)
        fieldset.LegendForeground = DirectCast(e.NewValue, SolidColorBrush)
    End Sub

    Public Property LegendForeground As SolidColorBrush
        Get
            Return DirectCast(Me.GetValue(LegendForegroundProperty), SolidColorBrush)
        End Get
        Set(ByVal value As SolidColorBrush)
            MyBase.SetValue(LegendForegroundProperty, value)
        End Set
    End Property
End Class

I apologize for the VB.NET code.

As I said, probably lots of better solutions but here goes.

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