我刚开始为Windows 8编程,我正在开发一个简单的记事本。我似乎找不出这个错误。
当文本框不再为空时,我希望启用保存按钮。我在文本框本身上使用了一个keyup事件:
<TextBox x:Name="TextBoxNote" KeyUp="TextBoxNote_KeyUp" />代码背后:
private void TextBoxNote_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (TextBoxNote.Text == "")
{
ApplicationBarSaveButton.IsEnabled = false;
}
else
{
ApplicationBarSaveButton.IsEnabled = true;
}
}在XAML中,我将IsEnabled设置为false
<shell:ApplicationBarIconButton x:Name="ApplicationBarSaveButton"
IconUri="/Assets/AppBar/e105-Save.76 (1).png"
Text="save" IsEnabled="False"
Click="ApplicationBarIconButton_Click_1" />如果我在文本框中输入了什么内容,就会得到以下错误:
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Notepad
StackTrace:
at Notepad.WriteNote.TextBoxNote_KeyUp(Object sender, KeyEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
InnerException: 在线:
ApplicationBarSaveButton.IsEnabled = true;对这个有什么想法吗?
发布于 2014-02-11 12:18:04
尝尝这个,
private void TextBoxNote_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
ApplicationBarIconButton button= (ApplicationBarIconButton)ApplicationBar.Buttons[0];
if (TextBoxNote.Text == "")
{
button.IsEnabled = false;
}
else
{
button.IsEnabled = true;
}
}https://stackoverflow.com/questions/21701119
复制相似问题