SilverLight学习笔记--建立Silverlight自定义控件(3)--操作控件内部成员
2009-07-17 10:44
543 查看
在上述自定义控件MySilverButton内部,我们有Rectangle(名为BodyElement)和TextBlock (名为ButtonCaption)两个内部成员,如果要对其操作,我们需要用到 GetTemplateChild 函数来达到目的。
在此,我们以修改TextBlock的Text属性值。加入代码到SimpleButton_MouseLeftButtonUp事件中
void SimpleButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (Click != null)
{
Click(this, new RoutedEventArgs());
TextBlock BgTextBlock = (sender as MySilverButton). ("ButtonCaption") as TextBlock;
BgTextBlock.Text = "修改显示文本";
}
}
MySilverButton.cs完整代码如下:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace MyDesignButton
{
public class MySilverButton: ContentControl
{
public event RoutedEventHandler Click; //添加Click事件
public MySilverButton()
{
this.DefaultStyleKey = typeof(MySilverButton);
this.MouseLeftButtonUp += new MouseButtonEventHandler(SimpleButton_MouseLeftButtonUp); //添加Click事件
}
//添加Click事件
void SimpleButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (Click != null)
{
Click(this, new RoutedEventArgs());
TextBlock BgTextBlock = (sender as MySilverButton). ("ButtonCaption") as TextBlock;
BgTextBlock.Text = "修改显示文本";
}
}
}
}
重新生成,并回到MySLbutton项目运行测试,可看到结果。
下一篇:
SilverLight学习笔记--建立Silverlight自定义控件(4)--添加自定义属性
前往:Silverlight学习笔记清单
在此,我们以修改TextBlock的Text属性值。加入代码到SimpleButton_MouseLeftButtonUp事件中
void SimpleButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (Click != null)
{
Click(this, new RoutedEventArgs());
TextBlock BgTextBlock = (sender as MySilverButton). ("ButtonCaption") as TextBlock;
BgTextBlock.Text = "修改显示文本";
}
}
MySilverButton.cs完整代码如下:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace MyDesignButton
{
public class MySilverButton: ContentControl
{
public event RoutedEventHandler Click; //添加Click事件
public MySilverButton()
{
this.DefaultStyleKey = typeof(MySilverButton);
this.MouseLeftButtonUp += new MouseButtonEventHandler(SimpleButton_MouseLeftButtonUp); //添加Click事件
}
//添加Click事件
void SimpleButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (Click != null)
{
Click(this, new RoutedEventArgs());
TextBlock BgTextBlock = (sender as MySilverButton). ("ButtonCaption") as TextBlock;
BgTextBlock.Text = "修改显示文本";
}
}
}
}
重新生成,并回到MySLbutton项目运行测试,可看到结果。
下一篇:
SilverLight学习笔记--建立Silverlight自定义控件(4)--添加自定义属性
前往:Silverlight学习笔记清单
相关文章推荐
- SilverLight学习笔记--如何在xaml文件中操作用户在后台代码定义的类(2)--示例篇:创建一个登录控件(原创)(转载本文请注明出处)
- SilverLight学习笔记--建立Silverlight自定义控件(1)--外观设计
- vs2010 学习Silverlight学习笔记(8):使用用户控件
- Android(java)学习笔记111:成员位置的内部类的介绍
- Silverlight学习笔记基本控件(三)
- [Silverlight学习笔记]如何获取ItemsControl的DataTemplate中定义的控件?
- SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
- SilverLight学习笔记--实际应用(一)(5):手把手建立一个Silverlight应用程序之异步数据校验2
- (原创)c#学习笔记10--定义类成员01--成员定义02--建立方法
- SilverLight学习笔记--关于使用IValueConvert对绑定数据的格式化操作
- 内部类学习笔记(一)成员内部类
- 【学习笔记】WebDriver操作第三方控件
- Silverlight学习笔记基本控件(四)
- Silverlight基本控件学习笔记(十)[HyperlinkButton]
- SilverLight学习笔记--实际应用(一)(2):手把手建立一个Silverlight应用程序之添加记录
- SilverLight学习笔记--实际应用(一)(7):手把手建立一个Silverlight应用程序之数据回滚
- Silverlight学习笔记:改变控件的样式
- Selenium学习笔记19-操作控件设置高亮
- WPF and Silverlight 学习笔记(十):WPF控件模型
- Silverlight 自定义控件模板管理 (silverlight 学习笔记)