XAMLでは、ListBoxに上下2つの要素があり、上段の要素は赤色のBorderです。
下段の要素も同じBorderなのですが、ListBoxItemの子要素として登録されており 、プロパティーでWidth="160" Height="50"が有効になっています。
下段の要素と同じWidth・ HeightのListBoxItemをプログラムで追加したいと思っていますが、ListBoxItemに子要素を追加する方法が分かりません。御教授願います。
◆XAML
<Window x:Class="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:WpfApp0703"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ListBox x:Name="LB1" HorizontalAlignment="Left" Height="371" Margin="27,23,0,0" VerticalAlignment="Top" Width="386">
<!-- ListBoxの要素にはなっているが、Width="160" Height="50"としたい -->
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="24" Margin="1,4,0,0" VerticalAlignment="Top"
Width="9" Background="#FFF06C6C"/>
<!-- これをプログラムでListBoxに追加するには? -->
<ListBoxItem Width="160" Height="50" HorizontalAlignment="Left">
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="24" Margin="1,4,0,0" VerticalAlignment="Top"
Width="9" Background="#FFF06C6C"/>
</ListBoxItem>
</ListBox>
</Grid>
</Window>
◆コード
Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim lb1 = Me.LB1
Dim li1 As New ListBoxItem
li1.Width = 160
li1.Height = 50
li1.HorizontalAlignment = HorizontalAlignment.Left
'ListBoxItemに子要素を追加する方法が無い
lb1.Items.Add(li1)
End Sub
End Class