WPF从复选框未选中事件返回值

时间:2015-12-21 23:58:12

标签: c# wpf visual-studio

如何在取消选中复选框时返回值?我注意到你需要创建一个转换器但是有更简单的方法吗?

XAML

<Grid>
        <TextBox x:Name="textBox4" Visibility="Hidden"  HorizontalAlignment="Left" Height="23" Margin="149,135,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="43" TextChanged="textBox4_TextChanged"/>
        <CheckBox x:Name="checkBox" Content="Party?(4 or more)" HorizontalAlignment="Left" Margin="33,135,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" Width="116" Height="23"/>

    </Grid>

C#

private void checkBox_Checked(object sender, RoutedEventArgs e)
        {
            Handle(sender as CheckBox);
        }
        private void checkBox_Unchecked(object sender, RoutedEventArgs e)
        {
            Handle(sender as CheckBox);
        }

        void Handle(CheckBox checkBox)
        {
            bool chkd = checkBox.IsChecked.Value;

            if (chkd)
            {
                textBox4.Visibility = Visibility.Visible;
                textBox6.IsEnabled = IsEnabled.Equals(false);
            }
            else
            {
                textBox4.Visibility = Visibility.Hidden;
            }

1 个答案:

答案 0 :(得分:-1)

在XAML中为Unchecked事件添加处理程序,您的代码将被调用。

段:

Unchecked="checkBox_Unchecked"

达成:

<CheckBox x:Name="checkBox" Content="Party?(4 or more)" HorizontalAlignment="Left" Margin="33,135,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" Unchecked="checkBox_Unchecked" Width="116" Height="23"/>

使用控件/事件并帮助发现可用内容时,请参阅属性窗口和事件,如下图所示:

enter image description here