Silverlight操作网页元素

Silverlight操作网页元素

还是通过网页元素的ID得到给元素,然后设置该元素的属性即可

Html

1
< img id = "myimg" src = "ClientBin/Images/Eagle.jpg" alt = "niao" />

XAML

1
2
3
< StackPanel >
        < Slider x:Name = "myslider" Maximum = "1000" Minimum = "0" Value = "50" ValueChanged = "myslider_ValueChanged" ></ Slider >
    </ StackPanel >

CS

1
2
3
4
5
private void myslider_ValueChanged( object sender, RoutedPropertyChangedEventArgs< double > e)
        {
            HtmlElement img = HtmlPage.Document.GetElementById( "myimg" );
            img.SetAttribute( "width" ,e.NewValue.ToString());
        }

反之呢?

我们要改变Silvelight中的元素,那么必须在后台事件中,所以,原理就是把html元素的事件绑定到code-behind即可

html

1
2
3
4
5
< select id = "select" >
    < option title = "Red" value = "Red" selected>Red</ option >
    < option title = "Blue" value = "Blue" >Blue</ option >
    < option title = "Yellow" value = "Yellow" >Yellow</ option >
    </ select >

XAML

1
< Ellipse x:Name = "myell" Width = "200" Height = "200" Fill = "Red" ></ Ellipse >

CS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public MainPage()
        {
            InitializeComponent();
            HtmlElement select = HtmlPage.Document.GetElementById( "select" );
            select.AttachEvent( "onchange" , new EventHandler<HtmlEventArgs>(Select_onChange));
        }
 
        private void Select_onChange( object sender, HtmlEventArgs e)
        {
            HtmlElement select = HtmlPage.Document.GetElementById( "select" );
            string Value = select.GetAttribute( "value" );
            switch (Value)
            {
                case "Red" :
                    myell.Fill = new SolidColorBrush(Colors.Red);
                    break ;
                case "Blue" :
                    myell.Fill = new SolidColorBrush(Colors.Blue);
                    break ;
                case "Yellow" :
                    myell.Fill = new SolidColorBrush(Colors.Yellow);
                    break ;
                default :
                    break ;
            }
        }

 

转载于:https://www.cnblogs.com/HelloMyWorld/archive/2013/01/07/2849440.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值