none
C# Birden fazla CheckBox kontrol etmek RRS feed

  • Soru

  • Merhaba, Window'da diyelim ki 5 tane checkBox var. Buton ekleyip butona tıklayınca Checked olan checkBox'ları UnChecked yapıcak. Tek tek hepsine kod yazmak uzun olurdu. Yardım ederseniz iyi olur :).
    4 Ekim 2015 Pazar 09:19

Yanıtlar

  • MainWindow.xaml

    <Window x:Class="WpfApplication1.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:WpfApplication1"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Grid Name="grid1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="50"/>
            </Grid.RowDefinitions>
            <CheckBox Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Stretch" Content="CheckBox1"/>
            <CheckBox Grid.Column="0" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Stretch" Content="CheckBox1"/>
            <CheckBox Grid.Column="0" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Stretch" Content="CheckBox1"/>
            <CheckBox Grid.Column="0" Grid.Row="3" VerticalAlignment="Center" HorizontalAlignment="Stretch" Content="CheckBox1"/>
            <Button Click="Button_Click" Grid.Row="4" Content="İşlemi Yap"/>
        </Grid>
    </Window>
    

    MainWindow.xaml.cs

    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                foreach (var item in grid1.Children)
                {
                    if(item is CheckBox)
                    {
                        CheckBox c = (CheckBox)item;
                        if (c.IsChecked == true)
                        {
                            c.IsChecked = false;
                        }
                    }
                }
            }
        }
    }



    .Net Developer

    5 Ekim 2015 Pazartesi 10:10