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 ;
            }
        }

 

你可能感兴趣的:(silverlight)