none
Listbox, DataTemplate and HorizontalContentAlignment

    Question

  • Hi all,

    using silverlight2 beta1, it seems that in a datatemplate of a listbox the ListBox.HorizontalContentAlignment is not supported.

     

    Is there a bug?

    Thursday, March 06, 2008 4:57 AM

Answers

  • Thanks for reporting this issue. It's a known bug which our developers are working on it.

    Sunday, March 09, 2008 11:53 PM
  • deltakosh,

    If this is the issue I have in mind, then the problem is caused by the Line element near the bottom of the ListBoxItem's default style in generic.xaml having Stretch=Fill.

    This is not an ideal solution, but you can provide your own Template for ListBoxItem (easily via ListBox.ItemContainerStyle and copy+paste) that does not have this Line and therefore does not have the alignment problem (at least in the scenario I looked earlier today).

    Hope this helps!

    Monday, March 10, 2008 8:38 PM

All replies

  • deltakosh,

    Could you please post a sample demonstrating the issue you're seeing? Below is a very simplified attempt at what I think you're getting at that demonstrates how HorizontalContentAlignment may not work quite the way one expects. In particular, this XAML shows a *centered* red rectangle on both WPF and Silverlight:

    <ItemsControl HorizontalContentAlignment="Right">
        <ItemsControl.Template>
            <ControlTemplate>
                <Border BorderBrush="Black" BorderThickness="1">
                    <ItemsPresenter/>
                </Border>
            </ControlTemplate>
        </ItemsControl.Template>
        <Rectangle Width="20" Height="20" Fill="Red"/>
    </ItemsControl>
    
    Thursday, March 06, 2008 4:48 PM
  • Hi David, I use a DataTemplate in my template.

    Here is the sample:

            <ListBox x:Name="lstSamples" Grid.Row="1" ItemsSource="{Binding ''}" HorizontalContentAlignment="Stretch">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Image Source="{Binding Preview}">
                            <Image.RenderTransform>
                                <ScaleTransform ScaleX="1" ScaleY="1" />
                            </Image.RenderTransform>
                        </Image>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

    Each item are left aligned and don't stretch at all.

     

    Am I wrong?

    Friday, March 07, 2008 3:43 AM
  • Thanks for reporting this issue. It's a known bug which our developers are working on it.

    Sunday, March 09, 2008 11:53 PM
  • deltakosh,

    If this is the issue I have in mind, then the problem is caused by the Line element near the bottom of the ListBoxItem's default style in generic.xaml having Stretch=Fill.

    This is not an ideal solution, but you can provide your own Template for ListBoxItem (easily via ListBox.ItemContainerStyle and copy+paste) that does not have this Line and therefore does not have the alignment problem (at least in the scenario I looked earlier today).

    Hope this helps!

    Monday, March 10, 2008 8:38 PM
  • I've tried to copy default template from http://msdn2.microsoft.com/en-us/library/cc278062(vs.95).aspx , but I got lot of errors:

    Error 1 'Library.App' does not contain a definition for 'FindName' and no extension method 'FindName' accepting a first argument of type 'Library.App' could be found (are you missing a using directive or an assembly reference?) 

    Error 2 The property 'Name' was not found in type 'GradientStop'. ( x 6 times)

    First error opens up "hidden" app.g.cs ....

    Same things happens when I copy this to App.Resources and use it via key, or copy in ListBox.ItemContainerStyle

    What I am doing wrong?

    Wednesday, April 02, 2008 10:54 AM
  • I found how to use default style for ItemContainerStyle. Style has to be in <ListBox.ItemContainerStyle>, and it works. Error with app.g.cs was showing because debuger had that file open during first scenario (to include Style in App.Resources). However I would like to find soultion how to resource this stule to use it accross controls.

    David Anson: Line is fine, removing Line won't solve Streching problem, but removing "hardcoded" HorizontalAlignment="Left" from ContentPresenter in ControlTemplate solved my problems and content is streching w/o problem.

     

    Wednesday, April 02, 2008 12:07 PM