- 浏览: 1153757 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
RebeccaZhong:
严重: StandardWrapper.Throwableco ...
三步发布java方式的rest服务 -
RebeccaZhong:
严重: StandardWrapper.Throwableco ...
三步发布java方式的rest服务 -
冷酷月光:
楼主。请教一下。arcgis for android 有提供地 ...
ArcGIS API For Android离线地图的实现 -
winney117:
请问如何GET已有网页上的指定内容?比如百度文库中的某一篇文章 ...
三步发布java方式的rest服务 -
zige1012:
您好,我想问问我想换个自己地图的切片,也有4层(L0-L3), ...
ArcGIS API For Android离线地图的实现
As a Silverlight control developer, you may encounter a need to create a custom content control. This type of control allows the consumers to place anything they want in certain areas of your control. In Silverlight, you may have noticed that the Button control has a content property. You can essentially put anything you want in a button. I’m going to show you how you can create a control to do this.
We are going to create a control that has a header and body. The final result will look like the image below:
Step 1 – Setup the Solution
The first step in creating our control is to ensure that our Visual Studio environment is setup correctly. We’ll have our basic Silverlight Application project and Web Project to test it in. However, we are going to add a Silverlight Class Library project and add a reference to it in our Silverlight Application project.
The next step is what gets a lot of developers. Add a “themes” folder in our controls project (Silverlight Class Library project). Inside the “themes” folder, add a new text file and name it “generic.xaml”. Make sure that the folder/file names are lower case!
Silverlight 2 does not support themes like WPF does. However, inserting the “themes/generic.xaml” file inside our controls project does yield us the ability to define our CustomContentControl style/template.
Step 2 – Create the Custom Content Control
Now that our solution is ready, let’s add our CustomContentControl to our controls project. We’ll be inheriting from the ContentControl object. Therefore by default, we’ll already have a Content property. However we want to create an additional Header property so that other developers can put whatever they want in the header. Here’s our CustomContentControl code:
Here we have our control which inherits from ContentControl and has a Header property defined. There’s one last thing to mention about our control. Check out line 17. We set the DefaultStyleKey to a resource object which we are going to define in our generic.xaml file. So let’s do that.
Step 3 – Create Control’s DefaultStyleKey (generic.xaml)
In step 2 we saw that after our CustomContentControl was instantiated we immediately wired up its DefaultStyleKey. In the generic.xaml file we’ll define our style/template for our CustomContentControl. The style will automatically be found and used for our control. Here’s the style for our control:
At a quick glance take note that we have a ContentControl for both the Header and Content properties of our CustomContentControl. When the template is bound, the Header and Content values will be placed inside our ContentControls defined here.
Step 4 – Use the CustomContentControl
Now that we have the solution setup, our control coded, and our default style/template defined in the generic.xaml file, we can use our control. Here’s the page XAML that uses the control.
If you wish to add events to your custom content control, I've written another post which can be found here.
We are going to create a control that has a header and body. The final result will look like the image below:
Step 1 – Setup the Solution
The first step in creating our control is to ensure that our Visual Studio environment is setup correctly. We’ll have our basic Silverlight Application project and Web Project to test it in. However, we are going to add a Silverlight Class Library project and add a reference to it in our Silverlight Application project.
The next step is what gets a lot of developers. Add a “themes” folder in our controls project (Silverlight Class Library project). Inside the “themes” folder, add a new text file and name it “generic.xaml”. Make sure that the folder/file names are lower case!
Silverlight 2 does not support themes like WPF does. However, inserting the “themes/generic.xaml” file inside our controls project does yield us the ability to define our CustomContentControl style/template.
Step 2 – Create the Custom Content Control
Now that our solution is ready, let’s add our CustomContentControl to our controls project. We’ll be inheriting from the ContentControl object. Therefore by default, we’ll already have a Content property. However we want to create an additional Header property so that other developers can put whatever they want in the header. Here’s our CustomContentControl code:
Public Class CustomContentControl Inherits ContentControl Public Shared ReadOnly HeaderProperty As DependencyProperty = DependencyProperty.Register("Header", GetType(UIElement), GetType(CustomContentControl), Nothing) Public Property Header() As UIElement Get Return DirectCast(Me.GetValue(CustomContentControl.HeaderProperty), UIElement) End Get Set(ByVal value As UIElement) Me.SetValue(CustomContentControl.HeaderProperty, value) End Set End Property Public Sub New() MyBase.New() Me.DefaultStyleKey = GetType(CustomContentControl) End Sub End Class
Here we have our control which inherits from ContentControl and has a Header property defined. There’s one last thing to mention about our control. Check out line 17. We set the DefaultStyleKey to a resource object which we are going to define in our generic.xaml file. So let’s do that.
Step 3 – Create Control’s DefaultStyleKey (generic.xaml)
In step 2 we saw that after our CustomContentControl was instantiated we immediately wired up its DefaultStyleKey. In the generic.xaml file we’ll define our style/template for our CustomContentControl. The style will automatically be found and used for our control. Here’s the style for our control:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ContentControlExample.Controls"> <!-- CustomContentControl --> <Style TargetType="local:CustomContentControl"> <Setter Property="Background" Value="Transparent" /> <Setter Property="Foreground" Value="Black" /> <Setter Property="BorderBrush" Value="Transparent" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="HorizontalAlignment" Value="Stretch" /> <Setter Property="VerticalAlignment" Value="Stretch" /> <Setter Property="HorizontalContentAlignment" Value="Left" /> <Setter Property="VerticalContentAlignment" Value="Top" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:CustomContentControl"> <Border Background="White" BorderBrush="#87AFDA" BorderThickness="1"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Border Grid.Column="0" Grid.Row="0" Background="#D4E6FC" BorderThickness="0,0,0,1" BorderBrush="#87AFDA"> <ContentControl Content="{TemplateBinding Header}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Foreground="#224499" FontWeight="Bold" FontFamily="Arial" FontSize="12" Margin="3,3,3,3" /> </Border> <ContentControl Grid.Column="0" Grid.Row="1" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" FontFamily="Arial" FontSize="{TemplateBinding FontSize}" FontStretch="{TemplateBinding FontStretch}" Foreground="{TemplateBinding Foreground}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" /> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
At a quick glance take note that we have a ContentControl for both the Header and Content properties of our CustomContentControl. When the template is bound, the Header and Content values will be placed inside our ContentControls defined here.
Step 4 – Use the CustomContentControl
Now that we have the solution setup, our control coded, and our default style/template defined in the generic.xaml file, we can use our control. Here’s the page XAML that uses the control.
<UserControl x:Class="ContentControlExample.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ex="clr-namespace:ContentControlExample.Controls;assembly=ContentControlExample.Controls" Width="300" Height="200"> <Grid x:Name="LayoutRoot" Background="White"> <ex:CustomContentControl x:Name="myCustomContentControl"> <ex:CustomContentControl.Header> <TextBlock Text="This is the Header" /> </ex:CustomContentControl.Header> <ex:CustomContentControl.Content> <StackPanel Orientation="Vertical" Margin="5"> <TextBlock Text="This is the body or content." Margin="0,0,0,5" /> <Button Content="Click Me!" Width="100" Height="30" /> </StackPanel> </ex:CustomContentControl.Content> </ex:CustomContentControl> </Grid> </UserControl>
If you wish to add events to your custom content control, I've written another post which can be found here.
发表评论
-
WCF Service中HttpContext.Current为null的解决办法
2010-12-28 17:36 2145http://s.click.taobao.com/t_8?e ... -
删除文件
2010-05-21 16:35 1226/// <summary> ... -
Silverlight 3中的Behavior技术
2010-04-15 18:52 1985转:http://blog.csdn.net/dotn ... -
C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值
2010-03-24 13:31 11393C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有 ... -
C#中几种获取路径方法
2010-03-22 14:37 3124string str1 =Process.GetCurrent ... -
c# rar压缩解压缩
2010-03-19 14:42 5284此程序利用 WinRAR 程序对文件进行压缩,命令行语法可参考 ... -
译文:详谈Silverlight 3.0中的数据验证 (Silverlight Validation in Detail)
2010-03-18 15:48 2550在先前的帖子里我提到 ... -
C# 读写XML文件
2010-03-17 15:26 1413phone.xml <?xml version=&quo ... -
C#XML读写
2010-03-09 13:00 3858一.前言: XML是微软.Ne ... -
Silverlight Make Into Control
2010-02-10 14:57 1314本文主要介绍了Expression ... -
简单的等待进度条
2010-02-10 10:58 4333最近我需要制作一些简 ... -
Silverlight 数据绑定(Binding)
2010-02-09 17:12 3764http://blog.csdn.net/banmuhuang ... -
Silverlight:Dependency Property(依赖属性)学习笔记
2010-02-09 13:07 5377学习SL/WPF,Dependency Prope ... -
silverlight中的几个冷门标记 {x:Null},d:DesignWidth,d:DesignHeight
2010-02-09 12:26 2278{x:Null}:用于设置某属 ... -
silverlight应用程序中未处理的错误代码:2104 类别:InitializeError 消息:无法下载 iis上部署Silverlight
2010-02-01 16:30 3977产生错误: 解决方案:第一步:默认网站--属性----- ... -
后台代码动态创建silverlight的Grid
2009-12-08 09:52 4843Code below can add item that co ... -
在 Silverlight 中使用 IValueConverter 实现对绑定数据的格式化
2009-11-11 20:31 3105转载:http://www.cnblogs.com/drive ... -
C# 中的委托和事件
2009-11-04 20:53 1217MyClass.cs using System; us ... -
Visual Studio 2008无法创建silverlight项目
2009-10-15 09:44 2112现象:xp + visual studio 2008 sp1安 ...
相关推荐
FlexGrid for Silverlight provides built-in printing, cell merging, column aggregation, custom cells, and all the extensibility you need for your business data. HTML and PDF Support ...
FlexGrid for Silverlight provides built-in printing, cell merging, column aggregation, custom cells, and all the extensibility you need for your business data. HTML and PDF Support Get first-...
FlexGrid for Silverlight provides built-in printing, cell merging, column aggregation, custom cells, and all the extensibility you need for your business data. HTML and PDF Support Get first-...
FlexGrid for Silverlight provides built-in printing, cell merging, column aggregation, custom cells, and all the extensibility you need for your business data. HTML and PDF Support ...
FlexGrid for Silverlight provides built-in printing, cell merging, column aggregation, custom cells, and all the extensibility you need for your business data. § HTML and PDF Support Get first-...
FlexGrid for Silverlight provides built-in printing, cell merging, column aggregation, custom cells, and all the extensibility you need for your business data. § HTML and PDF Support Get first-...
FlexGrid for Silverlight provides built-in printing, cell merging, column aggregation, custom cells, and all the extensibility you need for your business data. HTML and PDF Support ...
<UserControl x:Class="CustomMenu.SLContentMenu"> <ContentControl Content="{Binding MenuItem1Text}" /> <ContentControl Content="{Binding MenuItem2Text}"> </UserControl> ``` ...
在Silverlight中,开发者可以创建两种类型的自定义UI元素:用户控件(User Control)和自定义控件(Custom Control)。这两种控件都是为了封装和重用UI逻辑,但它们在设计和功能上有显著的区别。 **用户控件(User ...
FlexGrid for Silverlight provides built-in printing, cell merging, column aggregation, custom cells, and all the extensibility you need for your business data. HTML and PDF Support Get first-...
FlexGrid for Silverlight provides built-in printing, cell merging, column aggregation, custom cells, and all the extensibility you need for your business data. § HTML and PDF Support Get first-...
8.2 Creating a Custom Install Link; 8.3 Detecting Updates; 8.4 Detecting Out of Browser Status; 8.5 Detecting Network Connectivity and Availability; 8.6 Using Isolated Storage; 8.7 Enabling Elevated ...
##### 自定义标记扩展 (Custom Markup Extensions) 自定义标记扩展允许开发者创建自己的标记扩展,用于简化 XAML 编写并增强代码重用性。 示例代码如下: ```xml ``` ##### XAML 绑定调试 (XAML Binding ...
- **Silverlight and External Applications:** Describes how to integrate Silverlight applications and external applications with SharePoint. #### Extending Microsoft SharePoint 2010 **SharePoint ...
Get first-class support for HTML and PDF content in Silverlight with many of the ComponentOne controls. View your PDF documents and ...
Get first-class support for HTML and PDF content in Silverlight with many of the ComponentOne controls. View your PDF documents and ...
Get first-class support for HTML and PDF content in Silverlight with many of the ComponentOne controls. View your PDF documents and ...
It blends the application user interface, documents, and media content, while exploiting the full power of your computer's operating system. Its functionality extends to the support for Tablet PCs ...
Get first-class support for HTML and PDF content in Silverlight with many of the ComponentOne controls. View your PDF documents and ...