金冠招牌app:SilverLight 学习笔记建立Silverlight自定义控件(3)操作控件
在上述自定义控件MySilverButton内部,我们有Rectangle(名为BodyElement)和TextBlock (名为ButtonCaption)两个内部成员,假如要对其操作,我们必要用到 GetTemplateChild 函数来达到目的。
在此,我们以改动TextBlock的Text属性值。加入代码到SimpleButton_M金冠招牌appouseLeftButtonUp事故中
voi金冠招牌appd 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.金冠招牌appWindows.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 Cli金冠招牌appck; //添加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). ("ButtonC金冠招牌appaption") as TextBlock;
BgTextBlock.Text = "改动显示文本";
}
}
}
}
从新天生,并回到MySLbutton项目运行测试,可看到结果。