views:

121

answers:

3
+1  A: 

Your TextBlock has a height set of 40 units. If you remove that attribute altogether, you'll see the items spaced more naturally.

So your XAML would look more like this:

<Grid>
    <ItemsControl Name="announcmentsListBox" ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Path=Text}" />
                    <Button />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>
Drew Noakes
Sorry man, but I posted at first the incorrect codenow it's the real one
Erez
There is not enough information in your question to tell what's going on any more. For example, what type of object is being bound to the items control? What data templates are being picked up for that type?
Drew Noakes
Hi Drew,The object that is being bound to the items control is a List of a Class that I wrote , and the list inherits from ObservableCollection,I didn't use any DataTemplates.
Erez
@Erez - It looks as though some padding is being applied to your items. Remember that styles and templates can be inherited from the resource dictionaries of any logical parent! Search your code for any styles or templates applicable to your class or container types such as `ListBoxItem` (I can't remember the equivalent for `ItemsControl` offhand, if there even is one.)
Drew Noakes
A: 

I would advise you to use Mole visualizer to examine the generated items. Mole 4.2 can be downloaded from http://karlshifflett.wordpress.com/mole-for-visual-studio/

Just need to copy "Mole.Visualizer.dll" to appropriate visualizer folder.

If you are developing on Vista and are running with Elevated Security enabled, you must install Mole in the following directory.

* {VS Install path}\Common7\Packages\Debugger\Visualizers

All others download and unzip the above package. Copy the file in the Release package to either:

* My Documents\Visual Studio 2005\Visualizers {VS2005}
* My Documents\Visual Studio 2008\Visualizers {VS2008}

You can set a break point somewhere in the code which is easily accessible and then start the Mole visualizer on some object. Navigate the visual tree to the itemscontrol. Look for values in Margin and Padding for the generated TextBlocks. It will also tell you whether the values are inherited or local etc.

Trainee4Life
A: 

Use a Padding set to 0. Also, you can set Negative Margins which in many cases is the only way to coerce text into certain layouts (where Padding doesn't fit the bill, and container layout is non-overridable.)

You can also implement your own items panel template and redefine the layout of each item to compensate for the the padding being introduced, but this shouldn't be necessary. It would, however, afford you control over layout calculation for all items so that you can best-fit each item (and even go one step further and implement crazy new item layouts such as a carousel, as has been demoed by many pre-RTM videos of WPF/Avalon.)

Hope that helps, it's what I do.

Shaun Wilson
I suppose it's worth mentioning that you need to implement an items template to properly coerce a negative padding. Alternatively you can apply the settings on the parent, though in my experience this is something that you will wind up having to redo in the future. The proper way to display list items is to template them, assuming the default look-and-feel isn't adequate (or in your case, appears undesireable.)
Shaun Wilson