Silverlight学习笔记基本控件(五)

CheckBox的使用:

<UserControl x:Class="CheckBoxDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

        <StackPanel Orientation="Vertical" x:Name="SPanel">
            <TextBlock HorizontalAlignment="Left" Text="请选择你的爱好" FontSize="14"></TextBlock>
            <TextBlock  HorizontalAlignment="Left" x:Name="txtResult"></TextBlock>
            <CheckBox  HorizontalAlignment="Left" ToolTipService.ToolTip="民以食为天" IsChecked="True" Content="做饭"/>
            <CheckBox  HorizontalAlignment="Left" ToolTipService.ToolTip="为了家人,还是干吧"  Content="拖地"/>
            <CheckBox  HorizontalAlignment="Left" ToolTipService.ToolTip="健健康康好啊"  Content="洗衣服"/>
            <CheckBox  HorizontalAlignment="Left" ToolTipService.ToolTip="任重道远啊"  Content="抱孩子"/>
            <Button Content="提交" Click="Button_Click" HorizontalAlignment="Left" Width="150"/>
        </StackPanel>
    </Grid>
</UserControl>

后台:

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 CheckBoxDemo
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            txtResult.Text = "您选择了:";
            foreach (FrameworkElement element in SPanel.Children) {
               
                if(element.GetType()==typeof(CheckBox)){
                    if ((element as CheckBox).IsChecked==true) {
                        txtResult.Text += "["+(element as CheckBox).Content.ToString()+"]";
                    }
                }
            }
        }
    }
}

 

你可能感兴趣的:(silverlight)