wpf 在 C# 中运行时未选中复选框时禁用文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13135235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable text box when Check box is Unchecked during run time in C#
提问by Zarco
I have a CheckBox and a TextBox. During the runtime,
if the CheckBox is Checked then the TextBox is enabled.
我有一个复选框和一个文本框。在运行时,
如果选中 CheckBox,则启用 TextBox。
I did it using following code
我使用以下代码做到了
private void checkTime_Checked(object sender, RoutedEventArgs e)
{
if (checkTime.IsChecked == true)
{
txtTime_SR.IsEnabled = true;
}
}
What I need to do is, to disable the TextBox when the CheckBox is Unchecked during runtime.
我需要做的是,在运行时未选中 CheckBox 时禁用 TextBox。
Any idea of doing this ?
有没有这样做的想法?
回答by Kek
Reading your post and comments, I'll guess you are doing WPF or silverlight. Then, in that case, you may do it all in XAML :
阅读您的帖子和评论,我猜您正在使用 WPF 或 Silverlight。然后,在这种情况下,您可以在 XAML 中完成所有操作:
<CheckBox x:Name="checkTime" />
<TextBox x:Name="txtTime_SR" IsEnabled="{Binding IsChecked, ElementName=checkTime, Converter={StaticResource NotConverter}, Mode=OneWay}"/>
Then, you need to create the converter. This can be done by reading post here : How to bind inverse boolean properties in WPF?
然后,您需要创建转换器。这可以通过阅读此处的帖子来完成:How to bind inverse boolean properties in WPF?
Hope it helps
希望能帮助到你
回答by Dave New
If I understand, you want the textbox to be enabled when the checkbox is checked, and for it to be disabled when the checkbox is unchecked?
如果我理解,您希望在选中复选框时启用文本框,并在未选中复选框时禁用它?
private void checkTime_Checked(object sender, RoutedEventArgs e)
{
txtTime_SR.Enabled = checkTime.Checked;
}
Are you using the standard .NET TextBoxand CheckBoxcontrols?
您使用的是标准的 .NETTextBox和CheckBox控件吗?
EDIT: Ok, so it is WPF. Do this:
编辑:好的,所以它是 WPF。做这个:
private void checkTime_Checked(object sender, RoutedEventArgs e)
{
txtTime_SR.IsEnabled = checkTime.IsChecked;
}
回答by Ryan Loggerythm
Even simpler
更简单
<CheckBox Content="Include the following..." IsChecked="{Binding IncludeMyItems}" Name="checkBox_includeMyItems" />
<TextBox Name="myItems" Text="{Binding MyItems}" Height="23" Width="165" IsEnabled="{Binding IsChecked, ElementName=checkBox_includeMyItems}" />
回答by andy
private void checkTime_Checked(object sender, RoutedEventArgs e)
{
txtTime_SR.Enabled = checkTime.Checked;
}
OR
或者
private void checkTime_Checked(object sender, RoutedEventArgs e)
{
txtTime_SR.IsEnabled = checkTime.IsChecked;
}
回答by JordanTDN
I know this is an old post, however this should work. Use the Checked/Unchecked events.
我知道这是一个旧帖子,但是这应该有效。使用选中/未选中事件。
private void checkbox_otherflag_Unchecked(object sender, RoutedEventArgs e)
{
this.textbox_otherflagtext.IsEnabled = false;
}
private void checkbox_otherflag_Checked(object sender, RoutedEventArgs e)
{
this.textbox_otherflagtext.IsEnabled = true;
}

