Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用XAML/C向列表框项目添加内容#_C#_Xaml_Uwp_Listbox_Listboxitem - Fatal编程技术网

C# 使用XAML/C向列表框项目添加内容#

C# 使用XAML/C向列表框项目添加内容#,c#,xaml,uwp,listbox,listboxitem,C#,Xaml,Uwp,Listbox,Listboxitem,我刚刚开始使用VS2017和C#,并为Windows 10物联网设备(RPi)创建了一个应用程序。因此,我正在使用C#和XAML设计一个GUI的空白应用程序 我已经在XAML中完成了接口的完整设计,现在我正在尝试将数据填充到接口中,并创建一种魔力,让它工作;) XAML代码的一部分: <StackPanel x:Name="DetailHome" Grid.Column="1" Visibility="Visible"> <ListBox x:Name="Par

我刚刚开始使用VS2017和C#,并为Windows 10物联网设备(RPi)创建了一个应用程序。因此,我正在使用C#和XAML设计一个GUI的空白应用程序

我已经在XAML中完成了接口的完整设计,现在我正在尝试将数据填充到接口中,并创建一种魔力,让它工作;)

XAML代码的一部分:

<StackPanel x:Name="DetailHome" Grid.Column="1" Visibility="Visible">
        <ListBox x:Name="ParaBox" HorizontalAlignment="Left" Height="193" Margin="20,147,0,-340" VerticalAlignment="Top" Width="380" IsSynchronizedWithCurrentItem="False">
            <ListBoxItem x:Name="Parameter01" Content="Acceleration (mm/s²)"/>
            <ListBoxItem x:Name="Parameter02" Content=""/>
            <ListBoxItem x:Name="Parameter03" Content=""/>
            <ListBoxItem x:Name="Parameter04" Content=""/>
            <ListBoxItem x:Name="Parameter05" Content=""/>
            <ListBoxItem x:Name="Parameter06" Content=""/>
            <ListBoxItem x:Name="Parameter07" Content=""/>
            <ListBoxItem x:Name="Parameter08" Content=""/>
            <ListBoxItem x:Name="Parameter09" Content=""/>
            <ListBoxItem x:Name="Parameter10" Content=""/>
        </ListBox>
        <TextBox x:Name="txtBoxValue" HorizontalAlignment="Left" Margin="40,105,0,-140" Text="Value" VerticalAlignment="Center" Height="35" Width="345"/>
        <Button x:Name="BtnApply" Content="Apply" HorizontalAlignment="Left" Margin="20,60,0,-100" VerticalAlignment="Top" Height="40" Width="380"/>
        <TextBlock x:Name="lblSpeedValue" HorizontalAlignment="Center" Margin="287,354,37,-393" Text="Speed Value" Height="25" Width="100" VerticalAlignment="Center"/>
        <TextBlock x:Name="lblSpeed" HorizontalAlignment="Left" Margin="50,363,0,-393" Text="Calculated speed:" TextWrapping="WrapWholeWords" Height="30" Width="120" TextAlignment="Center" LineStackingStrategy="MaxHeight" OpticalMarginAlignment="None" IsDoubleTapEnabled="False" IsHoldingEnabled="False" IsRightTapEnabled="False" IsTapEnabled="False" TextReadingOrder="Default" TextTrimming="None"/>
    </StackPanel>
Parameters[n]
是一个数组,它保存我要显示的参数的数据。但是这不起作用,
参数01
没有被识别为我之前在XAML代码中定义的ListBoxItem


你能帮我弄清楚吗

应该使用ParaBox.ItemsSource=Parameters,并使用DisplayMemberPath获取每个listbox项中的Name对象,而不是在XAML中定义项,让它们动态填充

Parabox.ItemsSource = Parameters;
Parabox.DisplayMemberPath = nameof(yourType.Name);

这还允许您添加或删除参数,而无需更改xaml,只需从参数列表中添加或删除即可。

与此同时,我发现我不想使用正确的方法访问xaml对象


将代码移动到另一个方法(通过按下按钮等特定操作运行)是有效的

它是否适用于参数01.Content(大写C)?@Oystein不适用,也不适用。
Parabox.ItemsSource = Parameters;
Parabox.DisplayMemberPath = nameof(yourType.Name);