【问题标题】:checkbox can't be set initial value in xaml?复选框不能在 xaml 中设置初始值?
【发布时间】:2012-07-10 16:06:08
【问题描述】:

在 xaml 中输入代码为:

<CheckBox x:Name="cbxNew" Content="New" IsChecked="true"  Checked="cbxNew_Checked"  />

private void cbxNew_Checked(object sender, RoutedEventArgs e)
        {
            this.txtTest.Text = "";
        }

然后运行应用程序并得到错误:

Set Property
System.Windows.Controls.Primitives.ToggleButton.IsChecked threw an exception

非常惊讶。如何解决这个问题?

【问题讨论】:

  • 请提供一些周边XAML代码;上述行本身应该不会引起任何问题。另外,请提供更多异常详情。
  • 谢谢。更新了 Checked 事件的完整 xaml 和代码。

标签: silverlight silverlight-4.0 silverlight-5.0


【解决方案1】:

出现问题是因为 Checked 事件在对话框/页面的 InitializeComponent() 期间但在 txtTest 被分配值之前触发。

只需将代码更改为

private void cbxNew_Checked( object sender, RoutedEventArgs e )
{
  if ( this.txtTest != null )
    this.txtTest.Text = "";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 2011-03-07
    • 2018-05-04
    • 1970-01-01
    • 2018-02-27
    • 2013-09-21
    相关资源
    最近更新 更多