[英]How to add multiple items in same line to list box c#?
I need set two elements in one colon of list box but i can set only one with this code : 我需要在列表框的一个冒号中设置两个元素,但是我只能使用以下代码设置一个:
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<Rectangle Width="13" Height="13" Name="Rectangles" Margin="0,5,0,0" Fill="Red" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I want set Label beside rectange in same line too. 我也想在同一行的矩形旁边设置Label。
How can i do that? 我怎样才能做到这一点? Thank you
谢谢
Try placing it in a container item like Stackpanel or Grid 尝试将其放置在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>
Create Grid
inside the DataTemplate
and create ColumnDefinations
for you controls inside the ListView
. 在
DataTemplate
内部创建Grid
并为ListView
控件创建ColumnDefinations
。
<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>
You can try a stackpanel 您可以尝试一个堆栈面板
Example: 例:
<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>
The important part is the orientation of the stackpanel, if it is set to horizontal, items stack across rather than down. 重要的部分是堆栈面板的方向,如果将其设置为水平,则项目将堆叠而不是向下堆叠。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.