• [Silverlight]一个简单的GroupBox控件


    Silverlight没有提供GroupBox控件,自己动手写了一个。

    Generic.xaml文件:

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Sample" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib">
    
        <Style TargetType="local:GroupBox">
            <Setter Property="Background" Value="White"></Setter>
            <Setter Property="BorderBrush" Value="#687B8B"></Setter>
            <Setter Property="BorderThickness" Value="1"></Setter>
            <Setter Property="Padding" Value="6,10,6,6"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:GroupBox">
                        <Grid>
                            <Border Margin="0,8,0,0" CornerRadius="5"
                                    Background="{TemplateBinding Background}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}">
                                <ContentPresenter Margin="{TemplateBinding Padding}" ></ContentPresenter>
                            </Border>
                            <Border Margin="10,0,10,0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top"
                                    Background="{TemplateBinding Background}" >
                                <TextBlock Margin="5,0" Text="{TemplateBinding Title}" ></TextBlock>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>

    GroupBox.cs文件:

    using System.Windows;
    using System.Windows.Controls;
    
    namespace Sample
    {
        /// <summary>
        /// 分组框。
        /// </summary>
        public class GroupBox : ContentControl
        {
            public GroupBox()
            {
                this.DefaultStyleKey = typeof(GroupBox);
            }
    
            public static readonly DependencyProperty TitleProperty =
                DependencyProperty.Register("Title", typeof (string), typeof (GroupBox), null);
    
            /// <summary>
            /// 获取或设置标题。
            /// </summary>
            public string Title
            {
                get { return (string) GetValue(TitleProperty); }
                set { SetValue(TitleProperty, value); }
            }
        }
    }

    使用示例代码:

    <UserControl x:Class="AutoCompleteBoxSample.GroupBoxSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:Sample" Width="400" Height="300">
        <Grid x:Name="LayoutRoot" Margin="30" Background="White">
            <local:GroupBox Title="GroupBox的标题" >
                <Button Content="GroupBox的内容"></Button>                
            </local:GroupBox>
        </Grid>
    </UserControl>

    示例效果图:

    image

    2010.04.28 补充:

    如果使用Silverlight ToolKit,GroupBox类还可以直接从Silverlight ToolKit类库中的HeaderedContentControl类继承。改为从HeaderedContentControl类继承后,不仅代码更少,而且看上去更“Silverlight”一些。修改后的代码如下:

    Generic.xaml文件:

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Sample" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <Style TargetType="local:GroupBox">
            <Setter Property="Background" Value="White"></Setter>
            <Setter Property="BorderBrush" Value="#687B8B"></Setter>
            <Setter Property="BorderThickness" Value="1"></Setter>
            <Setter Property="Padding" Value="6,10,6,6"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:GroupBox">
                        <Grid>
                            <Border Margin="0,8,0,0" CornerRadius="5"
                                    Background="{TemplateBinding Background}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}">
                                <ContentPresenter Margin="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ></ContentPresenter>
                            </Border>
                            <Border Margin="10,0,10,0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top"
                                    Background="{TemplateBinding Background}" >
                                <ContentPresenter Margin="5,0" Content="{TemplateBinding Header}"></ContentPresenter>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>

    GroupBox.cs文件:

    using System.Windows.Controls;
    
    namespace AutoCompleteBoxSample
    {
        /// <summary>
        /// 分组框。
        /// </summary>
        public class GroupBox : HeaderedContentControl
        {
            public GroupBox()
            {
                this.DefaultStyleKey = typeof(GroupBox);
            }
        }
    }

    使用示例代码:

    <UserControl x:Class="AutoCompleteBoxSample.GroupBoxSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:Sample" Width="400" Height="300">
        <Grid x:Name="LayoutRoot" Margin="30" Background="White">
            <local:GroupBox Header="GroupBox的标题" >
                <Button Content="GroupBox的内容"></Button>                
            </local:GroupBox>
        </Grid>
    </UserControl>

    示例效果图与上图一样。当然,由于修改后的Header属性是object类型,所以如果你乐意的话,完全可以使用Button、Rectangle…做作GroupBox的Header,虽然我并不认为这是更好的做法。

  • 相关阅读:
    day23-json、pickle、configparser、hashlib、suprocess模块
    day22-时间模块、random模块、os模块、sys模块
    day21-py文件作用,导包、模块搜索路径
    day20-python-二分、面向过程思想、函数式、模块
    day19-python-叠加装饰器分析、yield、三元、生成式、递归
    day18-python-装饰器、迭代器、生成器
    day17-python-装饰器
    day16-python-函数对象、函数嵌套、闭包函数
    搭建yum本地源_阿里云CentOS服务器初始化设置
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql 启动不了
  • 原文地址:https://www.cnblogs.com/chinadhf/p/1722284.html
Copyright © 2020-2023  润新知