C# 使用单个复选框动态禁用一组文本框
MainWindow.xamlC# 使用单个复选框动态禁用一组文本框,c#,wpf,xaml,C#,Wpf,Xaml,MainWindow.xaml <CheckBox Content="Enable" Height="16" HorizontalAlignment="Left" Margin="190,40,0,0" Name="checkBox_Enable" VerticalAlignment="Top" IsChecked="True" Unchecked="checkBox_Enable_Unchecked" Checked="checkBox_Enable_Checked" />
<CheckBox Content="Enable" Height="16" HorizontalAlignment="Left" Margin="190,40,0,0" Name="checkBox_Enable" VerticalAlignment="Top" IsChecked="True" Unchecked="checkBox_Enable_Unchecked" Checked="checkBox_Enable_Checked" />
<Label Content="Fullscreen:" Height="15" HorizontalAlignment="Left" Margin="227,63,0,0" Name="label3" VerticalAlignment="Top" Width="56" Padding="1" />
<TextBox HorizontalAlignment="Right" Margin="0,86,243,0" Name="textBox_Hotkey_Fullscreen" Width="33" Height="18" VerticalAlignment="Top" />
<Label Content="Custom field:" HorizontalAlignment="Left" Margin="227,110,0,136" Name="label5" Padding="1" />
<TextBox Height="18" HorizontalAlignment="Left" Margin="227,131,0,0" Name="textBox_Hotkey_Customfield" VerticalAlignment="Top" Width="33" />
<Label Content="Window-related:" HorizontalAlignment="Left" Margin="227,155,0,87" Name="label4" Padding="1" />
<TextBox HorizontalAlignment="Left" Margin="227,0,0,112" Name="textBox_Hotkey_Windowrelated" Width="33" Height="18" VerticalAlignment="Bottom" />
为什么它在启动后立即抛出NullReferenceException
为什么它在启动后立即抛出NullReferenceException
因为当复选框
触发事件时,尚未加载所有控件
我建议将
复选框
绑定到bool,并将其他控件的绑定到bool(或绑定到复选框
)
为什么它在启动后立即抛出NullReferenceException
因为当复选框
触发事件时,尚未加载所有控件
我建议将复选框
绑定到一个bool,并将其他控件的绑定到该bool(或绑定到复选框
)。我建议像绑定一样使用(从xaml中删除事件处理程序)
对于每个要使用复选框启用/禁用的控件。我建议使用绑定(从xaml中删除事件处理程序)
对于要使用复选框启用/禁用的每个控件。是,这应该可以工作。当您有数据绑定时,您不需要处理事件。是的,这应该可以工作。在进行数据绑定时,不需要处理事件。
private void checkBox_Enable_Unchecked(object sender, RoutedEventArgs e)
{
textBox_Hotkey_Fullscreen.IsEnabled = false;
textBox_Hotkey_Customfield.IsEnabled = false;
textBox_Hotkey_Windowrelated.IsEnabled = false;
}
//There are no problems after purging this function. However, the controls couldn't be "re-enabled", what obviously makes my concept pointless.
private void checkBox_Enable_Checked(object sender, RoutedEventArgs e)
{
textBox_Hotkey_Fullscreen.IsEnabled = true;
textBox_Hotkey_Customfield.IsEnabled = true;
textBox_Hotkey_Windowrelated.IsEnabled = true;
}
IsEnabled="{Binding ElementName=checkBox_Enable, Path=IsChecked}"