开发者

Fixing a listbox's size in WPF

I have a Listbox that's inside a Grid that's inside a user control. That user control is placed in a tab panel that can resize.

The Listbox is defined开发者_JS百科 as:

<ListBox HorizontalAlignment="Left" Margin="12,42,12,12" Name="listBox1" VerticalAlignment="Top" >

This works great as long as my listbox is populated. When it's not populated, it's a square about 4 pixels wide/tall. If it's not full, it shrinks down to fit whatever is in it.

This is not what I'd like. What I would like is for it to always maintain the margins I've defined. My question is in two parts:

1: Why does it behave the way it does?

2: How do I make it behave the way I want?

Thanks.


Since you're placing this on a Grid control, you should actually use it as intended and create the rows and columns and put your ListBox in the correct cell(s).

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="42" />
        <RowDefinition />
        <RowDefinition Height="12" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="12" />
        <ColumnDefinition />
        <ColumnDefinition Width="12" />
    </Grid.ColumnDefinitions>

    <ListBox x:Name="listBox1"
             Grid.Row="1" Grid.Column="1">
    </ListBox>
</Grid>


It is maintaining the margins but the margins are outside the listbox itself so only change the positioning of the listbox relative to its container.

I'm not sure how you would like it to appear but could set a minimum width/height.

<ListBox HorizontalAlignment="Left" Margin="12,42,12,12" 
   Name="listBox1" MinHeight="10" MinWidth="100" VerticalAlignment="Top" />

or just remove the alignment settings

<ListBox Margin="12,42,12,12" Name="listBox1"  />

if you want the listbox to fill its container.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜