Pergunta

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.

How can i do that? Thank you

Foi útil?

Solução

Create Grid inside the DataTemplate and create ColumnDefinations for you controls inside the 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>

Outras dicas

Try placing it in a container item like Stackpanel or 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>

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top