提问人:Va_faill 提问时间:5/19/2022 最后编辑:Va_faill 更新时间:5/19/2022 访问量:495
WPF - 无法将列表框项添加到列表框(初级)
WPF - Can't add Listbox Item to Listbox (Beginner)
问:
当我尝试将列表框项添加到我的列表框(属性 -> 项 -> [...] -> 我从列表中选择 ListBoxItem -> 添加),然后我没有将项添加到列表中,而是在需要“搜索对象”的地方获得“选择对象”弹出窗口,所以我去:演示框架 -> System.Windows.Controls。-> ListBoxItem,然后我的窗体崩溃(System.NullReferenceException)
然后在这些信息下,我得到了一大堆我不明白的东西:
w System.Object.GetType()
w Microsoft.VisualStudio.DesignTools.Utility.Data.ShortToDoubleConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
w System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
w System.Windows.Data.BindingExpression.Activate(Object item)
w System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
w System.Windows.Data.BindingExpression.AttachOverride(DependencyObject target, DependencyProperty dp)
w System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject d, DependencyProperty dp)
w System.Windows.StyleHelper.GetInstanceValue(UncommonField`1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry)
w System.Windows.FrameworkTemplate.ReceivePropertySet(Object targetObject, XamlMember member, Object value, DependencyObject templatedParent)
w System.Windows.FrameworkTemplate.<>c__DisplayClass45_0.<LoadOptimizedTemplateContent>b__3(Object sender, XamlSetValueEventArgs setArgs)
w System.Xaml.XamlObjectWriter.OnSetValue(Object eventSender, XamlMember member, Object value)
w System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(ObjectWriterContext ctx, XamlMember prop, Object value, Boolean onParent)
w System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext ctx)
w System.Xaml.XamlObjectWriter.Logic_AssignProvidedValue(ObjectWriterContext ctx)
w System.Xaml.XamlObjectWriter.WriteEndObject()
w System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
w System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
w System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
w System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
w System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
w System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
w System.Windows.FrameworkElement.ApplyTemplate()
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.ContextLayoutManager.UpdateLayout()
w System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
w System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
w System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
w System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
w System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
w System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
我尝试在 XAML 中手动添加 ListBoxItem,但随后它带有下划线,VS 说:“使用 Items.Source 时操作无效。而是获取对元素的访问,并使用 ItemsControl.ItemsSource 属性更改它们。
代码:
<Window x:Class="ExperimentWithControls.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ExperimentWithControls"
mc:Ignorable="d"
Title="Eksperymenty z kontrolkami" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="Wpisz liczbę" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="18" RenderTransformOrigin="-0.24,-0.246" Margin="10,10,0,0"/>
<TextBlock x:Name="number" Grid.Column="1" HorizontalAlignment="Center" TextWrapping="Wrap" Text="Liczba" VerticalAlignment="Center" FontSize="24"/>
<TextBox x:Name="numberTextBox" HorizontalAlignment="Left" Margin="10,44,0,0" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" Width="120" FontSize="18" TextChanged="numberTextBox_TextChanged" PreviewTextInput="numberTextBox_PreviewTextInput"/>
<ListBox Margin="10,10,10,10" Name="myListBox" HorizontalAlignment="Center" VerticalAlignment="Center" d:ItemsSource="{d:SampleData ItemCount=5}">
</Grid>
</Window>
手动尝试过:
<ListBox Margin="10,10,10,10" Name="myListBox" HorizontalAlignment="Center" VerticalAlignment="Center" d:ItemsSource="{d:SampleData ItemCount=5}">
<ListBoxItem Content="1"></ListBoxItem>
</ListBox>
但这行不通。
我搜索过很多帖子,但每个人在从 C# 访问 Listbox 时都遇到问题,我只想使用 WPF 创建非常基本的表单。也许我没有安装某些东西,但奇怪的是,我可以将 ListBox 添加到窗体中,但无法添加 ListBox Item。
答:
-1赞
TheJuniorDeveloper
5/19/2022
#1
您可以尝试将列表框拖放到 WPF 窗体中。这使用 GUI 中的控件来填充它。
本教程确实帮助我从列表框项开始: https://wpf-tutorial.com/list-controls/listbox-control/
评论
0赞
Va_faill
5/19/2022
#2
谢谢你们,
顺便说一句,我删除了它是自动添加的,现在我可以添加 ListBox 项。d:ItemsSource="{d:SampleData ItemCount=5}
评论
d:ItemsSource