开发者

Silverlight grid: left should stretch, right is defined

开发者 https://www.devze.com 2023-04-11 10:31 出处:网络
I\'m trying to make a grid for a开发者_开发问答 listitem template. There should be three columns, the second and third columns should be 50px wide each, the first column should stretch too fill the re

I'm trying to make a grid for a开发者_开发问答 listitem template. There should be three columns, the second and third columns should be 50px wide each, the first column should stretch too fill the rest of the empty space.

Here's my code so far:

<DataTemplate x:key="NoteItemTemplate">
 <Grid Background="{Binding ColorBrush}"
   Height="50"
   Margin="5,5,5,5"
   HorizontalAlignment="Stretch">

   <Grid.ColumnDefinitions>
    <ColumnDefinition Width="1*" />
    <ColumnDefinition Width="50" />
    <ColumnDefinition Width="50" />
   </Grid.ColumnDefinitions>
 </Grid>
</DataTemplate>

      <!-- far away, in an another file.. -->

 <ListBox
    x:Name="NotesListBox"
    VerticalContentAlignment="Stretch"
    HorizontalContentAlignment="Stretch"
    VerticalAlignment="Top"
    ItemsSource="{Binding NotesList}"
    ItemTemplate="{StaticResource NoteItemTemplate}"
    Background="AliceBlue" />

Makes no difference if it's 1* or *.

But I can't seem to find the answer. How would you do it?


You mentioned that you want to use the above Grid definition as DataTemplate for an ListBoxItem. By default, the content of an ListBoxItem is not stretched, so that in your case you will only see 2 columns.

So I think you need to specify, that you want to stretch the content of the ListBoxItem. Do it like so:

<ListBox>
  <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>
0

精彩评论

暂无评论...
验证码 换一张
取 消