惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Schneier on Security
博客园 - 聂微东
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
有赞技术团队
有赞技术团队
腾讯CDC
博客园 - 叶小钗
WordPress大学
WordPress大学
博客园_首页
J
Java Code Geeks
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
Y
Y Combinator Blog
Schneier on Security
Schneier on Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
P
Privacy International News Feed
K
Kaspersky official blog
C
CERT Recently Published Vulnerability Notes
阮一峰的网络日志
阮一峰的网络日志
F
Full Disclosure
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
云风的 BLOG
云风的 BLOG
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threatpost
D
Docker
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
V2EX - 技术
V2EX - 技术
G
GRAHAM CLULEY
M
MIT News - Artificial intelligence
H
Heimdal Security Blog
N
News and Events Feed by Topic
P
Proofpoint News Feed

博客园 - 同一片海

Win10 资源管理器窗口无边框的问题 Android Studio发布Release版本之坑--Unknown host 'd29vzk4ow07wi7.cloudfront.net' 浏览器外部署Silverlight更新检查失败的原因及对策 C#调用非托管Dll时的参数传递 使用ATL开发ActiveX控件 大数据量传输时配置WCF的注意事项 Silverlight自定义主题 在Silverlight 3中使用主题 [Silverlight]Selector类到底有没有SelectedValue属性? WCF安全之ASP.NET兼容模式 WCF安全之customBinding [Silverlight]AutoCompleteBox控件的一个Bug? [Silverlight]DataGrid相关的几个小知识点 实现下拉列表支持DataGrid的AutoCompleteBox 可分片数据持久层-ShardingPL使用说明 Silverlight数据绑定中的可空类型与自定义转换器 一个丑陋的对Silverlight中的Grid无CellPadding的解决方案 Silverlight中的资源文件 - 同一片海 - 博客园 【分享】SqlServer数据库文档生成工具
[Silverlight]一个简单的GroupBox控件
同一片海 · 2010-04-27 · via 博客园 - 同一片海

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,虽然我并不认为这是更好的做法。