[英]Error on RadioButton Checked event
Here is xaml codes for your testing needs. 这是您的测试需求的xaml代码。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="200" Width="325">
<Grid>
<RadioButton x:Name="RadioButton1" IsChecked="True" GroupName="MyGroup" Content="Select" Margin="0,10,0,0"/>
<RadioButton x:Name="RadioButton2" GroupName="MyGroup" Content="Select" Margin="0,30,0,0"/>
<Menu x:Name="Menu1" HorizontalAlignment="Left" Margin="0,50,0,0">
<MenuItem x:Name="MenuItem1" Header="Saving">
<MenuItem x:Name="MenuItem2" Header="Save"/>
<MenuItem x:Name="MenuItem3" Header="Don't save"/>
</MenuItem>
</Menu>
</Grid>
</Window>
Following code is working great. 以下代码效果很好。
Class MainWindow
Private Sub RadioButton1_Unchecked(sender As Object, e As RoutedEventArgs) Handles RadioButton1.Unchecked
Menu1.IsEnabled = False
End Sub
End Class
Following code is not working. 下面的代码是不工作。 Do you know why?
你知道为什么吗?
Class MainWindow
Private Sub RadioButton1_Checked(sender As Object, e As RoutedEventArgs) Handles RadioButton1.Checked
Menu1.IsEnabled = True
End Sub
End Class
Error picture: 错误图片:
Because your RadioButton
node is above from the Menu
node. 因为您的
RadioButton
节点位于Menu
节点之上。 The Checked
event will occur before Menu1
created. Checked
事件将在创建Menu1
之前发生。 You have to check the Menu1
is not null
. 您必须检查
Menu1
不为null
。
if (null != Menu1)
Menu1.IsEnabled = true;
And place IsEnabled="True"
to Menu
node for default state. 并将
IsEnabled="True"
放置到Menu
节点以获取默认状态。
<Menu x:Name="Menu1" IsEnabled="True" HorizontalAlignment="Left" Margin="0,50,0,0">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.