当前位置:网站首页>Check控件

Check控件

2022-08-01 05:07:00 济南医疗小程序状元

Check控件

<Window x:Class="WpfApp1.control.Checkbox"
        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"
        xmlns:local="clr-namespace:WpfApp1.control"
        mc:Ignorable="d"
        Title="Checkbox" Height="450" Width="800">
    <Grid Name="gridMain">
        <Label     VerticalAlignment="Top" Width="150" FontSize="20" Background="Orange"
                 Foreground="White"  >MY 的选课</Label>
        <CheckBox Content="计算机组成原理" Margin="300,80" HorizontalAlignment="Left" 
                  VerticalAlignment="Top" Width="160" Height="30" FontSize="20"
                  IsChecked="True"></CheckBox>
        <CheckBox Content="网络信息" Margin="300,110" HorizontalAlignment="Left" 
                  VerticalAlignment="Top" Width="120" Height="30" FontSize="20"></CheckBox>
        <CheckBox Content="C language" Margin="300,50" HorizontalAlignment="Left" 
                  VerticalAlignment="Top" Width="120" Height="30" FontSize="20" BorderBrush="Blue"></CheckBox>
        <Button Content="获取我的选课" Click="Button_Click"  
                 Margin="331,182,0,0" HorizontalAlignment="Left" 
                  VerticalAlignment="Top" Width="120" Height="30" />
    </Grid>
</Window>

后台事件

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1.control
{
    /// <summary>
    /// Checkbox.xaml 的交互逻辑
    /// </summary>
    public partial class Checkbox : Window
    {
        public Checkbox()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // 等同于类的实例化 ,对象? 反正知道是处理那个XAML里面的数据,至少要知道的!
           UIElementCollection childrenobj= gridMain.Children; // 0.1 通过grid 属性名字,获取子元素方式!


            StringBuilder sbfobj = new StringBuilder("我的选课为:");
            
            // 进行遍历
            foreach (UIElement item in childrenobj)
            {
                // 判断
                if(item is CheckBox && (item as CheckBox).IsChecked.Value)
                {// 判断里面追加,已经获取到的字符串内容 我的选课内容

                    sbfobj.Append((item as CheckBox).Content+",");
                }
            }

            MessageBox.Show(sbfobj.ToString());
        
        
        
        }
    }
}

效果

 

  

原网站

版权声明
本文为[济南医疗小程序状元]所创,转载请带上原文链接,感谢
https://blog.csdn.net/chenggong9527/article/details/126086180