gpt4 book ai didi

c# - SelectedIndex 是空引用异常?

转载 作者:行者123 更新时间:2023-11-30 15:08:11 34 4
gpt4 key购买 nike

我一直收到这个错误:

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:

在代码处:

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.SelectedIndex == 1)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Center);
}
if (TextAlignment_combobox.SelectedIndex == 2)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Right);
}
}

使用 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>

最佳答案

好的,我已经测试了这个场景并且发现了你的问题。当您最初启动 WPF 应用程序时,它会通过 SelectionChanged 事件运行。一旦创建了 ComboBox 对象,就会发生这种情况。问题是,在您的 WPF 应用程序上,XAML 中的组合框位于您的 RichTextBox 之前。这意味着此事件在创建 RichTextBox 之前触发。因此,您会得到一个 Null 引用异常。你有两个选择。您可以吃掉错误或尝试确定 RichTextBox 是否存在,然后再尝试对其进行操作,或者您可以在 XAML 中将 RichTextBox 向上移动到 ComboBox 上方。这与表单放置无关,而是与 XAML 中的放置有关。任何一个都可以解决您的问题。

关于c# - SelectedIndex 是空引用异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5892149/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com