MainWindow.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" />
<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" />
MainWindow.xaml.cs
MainWindow.xaml.cs
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;
}
Why does it throw the NullReferenceException just after launch?
为什么在启动后抛出NullReferenceException ?
2 个解决方案
#1
4
Why does it throw the NullReferenceException just after launch?
为什么在启动后抛出NullReferenceException ?
Because not all controls are loaded yet when the CheckBox
fires the event.
因为当复选框触发事件时,并不是所有的控件都被加载。
I would suggest binding the CheckBox
to a bool and binding the other controls' IsEnabled
to that (or bind directly to the CheckBox
).
我建议将复选框绑定到bool并绑定其他控件的启用(或者直接绑定到复选框)。
#2
5
I would suggest use Binding like (remove event handlers from xaml)
我建议使用绑定like(从xaml中删除事件处理程序)
IsEnabled="{Binding ElementName=checkBox_Enable, Path=IsChecked}"
for every control you want to enable/disable with the checkbox.
对于您希望使用复选框启用/禁用的每个控件。
#1
4
Why does it throw the NullReferenceException just after launch?
为什么在启动后抛出NullReferenceException ?
Because not all controls are loaded yet when the CheckBox
fires the event.
因为当复选框触发事件时,并不是所有的控件都被加载。
I would suggest binding the CheckBox
to a bool and binding the other controls' IsEnabled
to that (or bind directly to the CheckBox
).
我建议将复选框绑定到bool并绑定其他控件的启用(或者直接绑定到复选框)。
#2
5
I would suggest use Binding like (remove event handlers from xaml)
我建议使用绑定like(从xaml中删除事件处理程序)
IsEnabled="{Binding ElementName=checkBox_Enable, Path=IsChecked}"
for every control you want to enable/disable with the checkbox.
对于您希望使用复选框启用/禁用的每个控件。