如何将同一行中的多个项目添加到列表框c#?
我需要在列表框的一个冒号中设置两个元素,但我只能使用以下代码设置一个:如何将同一行中的多个项目添加到列表框c#?,c#,listbox,items,C#,Listbox,Items,我需要在列表框的一个冒号中设置两个元素,但我只能使用以下代码设置一个: <ListView> <ListView.ItemTemplate> <DataTemplate> <Rectangle Width="13" Height="13" Name="Rectangles" Margin="0,5,0,0" Fill="Red" /> </DataTemplate>
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<Rectangle Width="13" Height="13" Name="Rectangles" Margin="0,5,0,0" Fill="Red" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我想在同一行的rectange旁边设置标签
我该怎么做?谢谢试着把它放在像Stackpanel或Grid这样的容器中
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Rectangle Width="13" Height="13" Name="Rectangles" Margin="0,5,0,0" Fill="Red" />
<TextBlock />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在
数据模板中创建网格
,并为列表视图中的控件创建列定义
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="13" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Rectange Grid.Column="0" Fill="Red" Margin="0,5,0,0"/>
<Label Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
您可以尝试stackpanel
例如:
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Width="13" Height="13" Name="Rectangles" Margin="0,5,0,0" Fill="Red" />
<Label Content="MyContent" />
<StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
重要的部分是stackpanel的方向,如果将其设置为水平,则项目会横向堆叠,而不是向下堆叠