Lesson 3: Events Part(I,II) 2008-06-25 17:02

字号:    

In silverlight beta2, objects that drectly derive from UI element like checkbox or button, if you click on them, the mouse event is handled by the control and is n't bubbled at all. so we should replace the MouseLeftButtonDown with click.

Page.Xaml code:

<UserControl x:Class="SilverlightApplication7.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="450" Height="300">
    <Grid x:Name="LayoutRoot" Background="Bisque" ShowGridLines="False">
        <Grid.RowDefinitions>
            <RowDefinition Height="10"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="10"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="10"/>
            <ColumnDefinition Width="250"/>
            <ColumnDefinition Width="15"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="10"/>
        </Grid.ColumnDefinitions>
       
        <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="1"
                    HorizontalAlignment="Right" VerticalAlignment="Bottom">
            <Button x:Name="ButtonOne" Height="30" Width="75"
                    Background="Beige" Content="Push Me" HorizontalAlignment="Right"
                    VerticalAlignment="Bottom"/>
            <Button x:Name="ButtonTwo" Height="30" Width="75"
                    Background="Blue" Margin="10,0,0,0"
                    Content="No, Push Me!" HorizontalAlignment="Right"
                    VerticalAlignment="Bottom"/>
        </StackPanel>
       
        <TextBlock x:Name="ButtonOneResponse" Text="Waiting..."
                   Grid.Row="1" Grid.Column="3" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
       
        <Border BorderBrush="Black" BorderThickness="1"
                Grid.Row="2" Grid.Column="1" VerticalAlignment="Bottom" Height="30" />
        <StackPanel x:Name="Mood" Orientation="Horizontal" Grid.Row="2" Grid.Column="1" VerticalAlignment="Bottom">
            <RadioButton x:Name="Quiet" Content="Quiet" IsChecked="true"
                         Width="58" Height="30" GroupName="Mood" Margin="5,0,0,0"/>
            <RadioButton x:Name="Active" Content="Active"
                         Width="58" Height="30" GroupName="Mood"/>
            <RadioButton x:Name="Noisy" Content="Noisy"
                         Width="58" Height="30" GroupName="Mood"/>
            <RadioButton x:Name="Frenetic" Content="Frenetic"
                         Width="70" Height="30" GroupName="Mood"/>
        </StackPanel>
        <TextBlock x:Name="MoodEffect" Grid.Row="2" Grid.Column="3" Text="Waiting..."
                   VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
       
        <Button x:Name="SetFeatures" Height="40" Width="250" Background="Blue"
                Grid.Row="3" Grid.Column="1">
            <Button.Content>
                <StackPanel x:Name="Features" Orientation="Horizontal" Grid.Row="3" Grid.Column="1"
                            VerticalAlignment="Bottom">
                    <CheckBox x:Name="Soft" Content="Soft" Width="50" Height="40"
                              Margin="5,0,0,0"/>
                    <CheckBox x:Name="Cozy" Content="Cozy" Width="50" Height="40"/>
                    <CheckBox x:Name="Warm" Content="Warm" Width="60" Height="40"/>
                    <CheckBox x:Name="Happy" Content="Happy" Width="60" Height="40"/>
                </StackPanel>
            </Button.Content>
        </Button>
        <TextBlock x:Name="FeaturesEffects" Grid.Row="3" Grid.Column="3"
                   Text="Waiting..." VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
        <Slider x:Name="theSlider" Height="20" Width="250"
                Minimum="0" Maximum="100" SmallChange="5" Value="10"
                Grid.Row="4" Grid.Column="1" HorizontalAlignment="Right" />
        <TextBlock x:Name="SliderEffect" Grid.Row="4" Grid.Column="3" Text="Waiting..."
                   VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
    </Grid>
</UserControl>
Page.Xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication7
{
    public partial class Page : UserControl
    {
        private int counterOne = 0;
        private int counterTwo = 0;
        private int counterThree = 0;
        public Page()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            ButtonOne.Click += new RoutedEventHandler(ButtonOne_Click);
            ButtonTwo.Click += new RoutedEventHandler(ButtonTwo_Click);
            Quiet.Click += new RoutedEventHandler(Mood_Click);
            Active.Click+=new RoutedEventHandler(Mood_Click);
            Noisy.Click+=new RoutedEventHandler(Mood_Click);
            Frenetic.Click+=new RoutedEventHandler(Mood_Click);
            SetFeatures.Click += new RoutedEventHandler(SetFeatures_Click);
            theSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(theSlider_ValueChanged);
        }

        void SetFeatures_Click(object sender, RoutedEventArgs e)
        {
            FeaturesEffects.Text = "Button Clicked " + (++counterTwo).ToString();
        }

        void theSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            SliderEffect.Text = theSlider.Value.ToString("N");
        }

        void Mood_Click(object sender, RoutedEventArgs e)
        {
            RadioButton rb = e.Source as RadioButton;
            MoodEffect.Text = rb.Content.ToString();
        }

        void ButtonTwo_Click(object sender, RoutedEventArgs e)
        {
            ButtonOneResponse.Text = "Button Two Pressed " + (++counterTwo).ToString();
        }

        void ButtonOne_Click(object sender, RoutedEventArgs e)
        {
            ButtonOneResponse.Text = "Button One pressed " + (++counterOne).ToString();
        }
    }
}

27次阅读
 
打火机男人的博客
力求原创
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
网易公司版权所有 ©1997-2009