I keep getting this error:
System.NullReferenceException was unhandled by user code
Message=[Arg_NullReferenceException]
Arguments:
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=5.0.60401.00&File=mscorlib.dll&Key=Arg_NullReferenceException
StackTrace:
at Jantire.DoHomeworkView.TextAlignment_combobox_SelectionChanged(Object sender, SelectionChangedEventArgs e)
at System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArgs e)
at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedItems, List`1 selectedItems)
at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
at System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e)
at System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
at System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
at System.Windows.Controls.ItemCollection.NotifyCollectionReady()
at System.Windows.Controls.ItemsControl.NotifyAllItemsAdded(IntPtr nativeItemsControl)
InnerException:
at the code:
private void TextAlignment_combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//This next line is where error is at
if (TextAlignment_combobox.SelectedIndex == 0)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Left);
}
if (TextAlignment_combobox.SelectedIn开发者_如何学编程dex == 1)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Center);
}
if (TextAlignment_combobox.SelectedIndex == 2)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Right);
}
}
With the XAML:
<ComboBox Width="128" x:Name="TextAlignment_combobox" SelectionChanged="TextAlignment_combobox_SelectionChanged" ToolTipService.ToolTip="Text Alignment">
<ComboBoxItem Name="LeftAlignment_comboboxitem" Content="Left Alignment" IsSelected="True"/>
<ComboBoxItem Name="CenterAlignment_comboboxitem" Content="Center Alignment"/>
<ComboBoxItem Name="RightAlignment_comboboxitem" Content="Right Alignment"/>
</ComboBox>
OK, I have tested this scenario out and I have found your issue. When you initially fire the WPF application, it runs through the SelectionChanged event. This happens as soon as the ComboBox object is created. The problem is that on your WPF application, you have your ComboBox in the XAML before your RichTextBox. That means that this event fires before the RichTextBox is created. Thus, you get a Null reference exception. You have two options. You could eat the error or try to determine if the RichTextBox exists before you attempt to operate on it or you could move the RichTextBox up in the XAML to be above your ComboBox. This has nothing to do with form placement but instead with placement within the XAML. Either one will solve your issue.
Without more detail, it sounds like EssayContents_richtextbox is null.
Either:
TextAlignment_combobox
is null (seems very unlikely)EssayContents_richtextbox
is null- or
EssayContents_richtextbox.Selection
is null
You should debug this and check them all until you find something thats null, or failing that protect against them being null in your code somehow, for example:
private void TextAlignment_combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (EssayContents_richtextbox == null || EssayContents_richtextbox.Selection == null)
{
// Handle me, or just
return;
}
if (TextAlignment_combobox.SelectedIndex == 0)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Left);
}
if (TextAlignment_combobox.SelectedIndex == 1)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Center);
}
if (TextAlignment_combobox.SelectedIndex == 2)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Right);
}
}
I'd guess that if EssayContents_richtextbox
is null this is an error in your code - also looking at the documentation for the Selection
property it seems that this will probably have a value even if there is no selection (although it doesn't explicitly say that it will never return null)
精彩评论