본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

checkbox 체크 여부 확인 ( checkbox / 스킨 / 디자인 / XAML )

728x90
반응형


checkbox 체크 여부 확인 ( checkbox / 스킨 / 디자인 / XAML )



testListBox.zip





MainWindow.xaml

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<Window x:Class="testListBox.MainWindow"
        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:testListBox"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
            <StackPanel>
                <GroupBox Header="GroupBox1" VerticalAlignment="Top" Height="100">
                    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
                        <StackPanel x:Name="StackPanelGroup1" VerticalAlignment="Top">
                            <CheckBox Content="하나 "/>
                            <CheckBox Content="둘 "/>
                            <CheckBox Content="셋 "/>
                            <CheckBox Content="넷 "/>
                        </StackPanel>
                    </ScrollViewer>
                </GroupBox>
                <Button x:Name="btn1" Content="Button" HorizontalAlignment="Center" VerticalAlignment="Top" Width="80" Height="30" Click="Button_Click"/>
 
                <GroupBox Header="GroupBox2" VerticalAlignment="Top" Height="100">
                    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
                        <StackPanel x:Name="StackPanelGroup2" VerticalAlignment="Top">
                            <CheckBox Content="CheckBox"/>
                            <Label Content="Label" Background="#FFE6E6E6"/>
                            <CheckBox Content="CheckBox"/>
                            <Label Content="Label" Background="#FFE6E6E6"/>
                            <CheckBox Content="CheckBox"/>
                            <Label Content="Label" Background="#FFE6E6E6"/>
                            <CheckBox Content="CheckBox"/>
                            <Label Content="Label"/>
                        </StackPanel>
                    </ScrollViewer>
                </GroupBox>
                <Button x:Name="btn2" Content="Button" HorizontalAlignment="Center" VerticalAlignment="Top" Width="80" Height="30" Click="Button_Click"/>
 
                <GroupBox Header="GroupBox3" VerticalAlignment="Top" Height="100">
                    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
                        <StackPanel x:Name="StackPanelGroup3" VerticalAlignment="Top">
                            <CheckBox Content="CheckBox"/>
                            <Label Content="Label" Background="#FFE6E6E6"/>
                            <CheckBox Content="CheckBox"/>
                            <Label Content="Label" Background="#FFE6E6E6"/>
                            <CheckBox Content="CheckBox"/>
                            <Label Content="Label" Background="#FFE6E6E6"/>
                            <CheckBox Content="CheckBox"/>
                            <Label Content="Label"/>
                        </StackPanel>
                    </ScrollViewer>
                </GroupBox>
                <Button x:Name="btn3" Content="Button" HorizontalAlignment="Center" VerticalAlignment="Top" Width="80" Height="30" Click="Button_Click"/>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</Window>
 
cs




MainWindow.xaml.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
 
namespace testListBox
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        
        string msg;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
 
            if (btn.Name == "btn1")
            {
                // Group1 의 체크박스
                IEnumerable<CheckBox> ChkBoxes = from checkbox in this.StackPanelGroup1.Children.OfType<CheckBox>()
                                                       // where checkbox.IsChecked.Value 체크된 Checkbox 만 선택할때
                                                   select checkbox;
 
                // 체크된 content 값 가져오기
                msg = "";
                foreach (CheckBox Chkbox in ChkBoxes)
                {
                    if (Chkbox.IsChecked == true)
                    {
                        msg += Chkbox.Content.ToString();
                    }
                }
                MessageBox.Show(msg);
 
                // 전체 체크하기
                foreach (CheckBox Chkbox in ChkBoxes)
                {
                    Chkbox.IsChecked = true;
                }
 
                // 전체 체크해제하기
                foreach (CheckBox Chkbox in ChkBoxes)
                {
                    Chkbox.IsChecked = false;
                }
            }
        }
    }
}
 
cs



자료를 여러개 찾아봤는데, 이게 가장 확실하고 나은거 같다.

참고자료를 토대로 작성해보았다.

역시 고수들은 따로 있다.





참고자료

https://stackoverflow.com/questions/2532106/selected-checkbox-in-wpf



관련 자료


http://www.wpf-tutorial.com/basic-controls/the-checkbox-control/

728x90
반응형