Search code examples
c#xamluwplistboxlistboxitem

Add content to Listbox Items using XAML/C#


I just started working with VS2017 and C# and creating an application for a Windows 10 IoT device (RPi). So I'm working in a blank app with C# and XAML to design an GUI.

I already did the full design of the interface in XAML, now I'm trying to fill the data into the interface and create the magic to let the thinks working ;).

Part of the XAML-code:

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

The content of the ListBoxItems isn't fixed. It can change, so I need to fill the content using the c#-file. In the XAML code above, there is just one Item already filled for example with the text "Acceleration (mm/s²)" as content. Because the ListBoxItems are named, I try the following C# code:

Parameter01.content = Parameters[0].Name;

Parameters[n] is an array that holds the data of the parameter that I want to display. But this doens't work, the Parameter01 isn't recognized as the ListBoxItem that I defined before in the XAML code.

How can help me clear this out?


Solution

  • In the meantime I found out that I wans't in the correct method to gain access to the XAML-objects!

    Moving my code to another method (that runs by an certain action like a pressed button) works.