1
votes

I have a ListBox with horizontal scrolling, and each item consists of a two row grid. The first row contains an image, and the second row a border used to render a reflection of the first.
(Yeah, yeah. I know. Yet another coverflow attempt...)

I need some help with the sizing of the images. If I don't specify any size, it will render the image at full size, but I want it to be restricted by height of the grid row. If the window resizes, the images should resize.

Any clues?

Update:
I have now changed the code a little. First of all I have removed a unnecessary trigger, but the important part is

  • Disabled vertical scrollbar on listbox.
  • Removed height on coverImage
  • Changed from layoutTransformation to RenderTransformation
  • Shrink non-selected item instead of scaling selected item.

This gives me nearly what I want. There is a gap between the coverImage and the coverReflection that I cant find a reason for. Any clues for that, og maybe I should post a new question...?

Second Update:
I think I have a solution for the reflection-gap to now. It feels a little awkward, though. I guess there are better ways of doing it.

What I've done is - I'm no longer flipping the border, I'm flipping the visual brush instead. - I have added a TileMode="Tile" for the visual brush

Now, I'm not sure why this works, but it's coming close to what I want, so...

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    x:Class="UntitledProject1.Window1"
    x:Name="Window"
    Title="Window1"
    Width="801" Height="786">
    <Window.Resources>
        <XmlDataProvider x:Key="dataProvider" XPath="Bilder">
            <x:XData>
                <Bilder xmlns="">
                    <Bilde>75760-1_-8589666289339775808.jpg</Bilde>
                    <Bilde>73255-3_-8589662994232744558.jpg</Bilde>
                    <Bilde>75760-1_-8589666289339775808.jpg</Bilde>
                    <Bilde>73255-3_-8589662994232744558.jpg</Bilde>
                    <Bilde>75760-1_-8589666289339775808.jpg</Bilde>
                    <Bilde>73255-3_-8589662994232744558.jpg</Bilde>
                </Bilder>
            </x:XData>
        </XmlDataProvider>

        <ControlTemplate x:Key="listControlTemplate" TargetType="{x:Type ListBoxItem}">
            <Grid x:Name="listItemGrid">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>      
                <Image x:Name="coverImage" 
                       Source="{Binding Path=InnerText}"  
                       Stretch="Uniform" 
                       HorizontalAlignment="Stretch" 
                       VerticalAlignment="Bottom"  
                       Grid.Row="0" 
                       RenderTransformOrigin="0.5,1">
                    <Image.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="0.7" ScaleY="0.7"/>
                            <SkewTransform AngleX="0" AngleY="0"/>
                            <RotateTransform Angle="0"/>
                            <TranslateTransform X="0" Y="0"/>
                        </TransformGroup>
                    </Image.RenderTransform>
                </Image>
                <Border x:Name="coverReflection" 
                        RenderTransformOrigin="0.5,0" 
                        Height="{Binding Path=ActualHeight, ElementName=coverImage, Mode= Default}" 
                        VerticalAlignment="Top" 
                        Grid.Row="1"
                        >
                    <Border.OpacityMask>
                        <LinearGradientBrush EndPoint="0.0,1" StartPoint="0.0,0">
                            <GradientStop Color="#00000000" Offset="0.6"/>
                            <GradientStop Color="#BBFFFFFF" Offset="0"/>
                        </LinearGradientBrush>
                    </Border.OpacityMask>
                    <Border.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="0.7" ScaleY="0.7"/>
                            <SkewTransform AngleX="0" AngleY="0"/>
                            <RotateTransform Angle="0"/>
                            <TranslateTransform X="0" Y="0"/>
                        </TransformGroup>
                    </Border.RenderTransform>
                    <Border.Background>
                        <VisualBrush Visual="{Binding ElementName=coverImage}" TileMode="Tile">
                            <VisualBrush.Transform>
                                <TransformGroup>
                                    <ScaleTransform ScaleX="1" ScaleY="-1"/>
                                    <SkewTransform AngleX="0" AngleY="0"/>
                                    <RotateTransform Angle="0"/>
                                    <TranslateTransform X="0" Y="0"/>
                                </TransformGroup>                               
                            </VisualBrush.Transform>
                        </VisualBrush>
                    </Border.Background>
                </Border>               
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsSelected" Value="True">                    
                    <Setter Property="RenderTransform" TargetName="coverImage">
                        <Setter.Value>
                            <TransformGroup>
                                <ScaleTransform ScaleX="1" ScaleY="1"/>
                                <SkewTransform AngleX="0" AngleY="0"/>
                                <RotateTransform Angle="0"/>
                                <TranslateTransform X="0" Y="0"/>
                            </TransformGroup>
                        </Setter.Value>
                    </Setter>                   
                    <Setter Property="RenderTransform" TargetName="coverReflection">
                        <Setter.Value>
                            <TransformGroup>
                                <ScaleTransform ScaleX="1" ScaleY="1"/>
                                <SkewTransform AngleX="0" AngleY="0"/>
                                <RotateTransform Angle="0"/>
                                <TranslateTransform X="0" Y="0"/>
                            </TransformGroup>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <Style TargetType="{x:Type ListBoxItem}" x:Key="listStyle">
            <Setter Property="Template" Value="{StaticResource listControlTemplate}" />
        </Style>
    </Window.Resources>
    <Window.BindingGroup>
        <BindingGroup/>
    </Window.BindingGroup>

    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource dataProvider}, XPath=/Bilder/Bilde}">
        <ListBox ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                 ScrollViewer.VerticalScrollBarVisibility="Disabled"
                 ItemsSource="{Binding }" 
                 IsSynchronizedWithCurrentItem="True" 
                 Background="#FF000000"
                 ItemContainerStyle="{StaticResource listStyle}" 
                 VerticalAlignment="Stretch"
                 >
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel IsItemsHost="True" Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
    </Grid>
</Window>
1

1 Answers

1
votes

Modify the style for the ListBoxItem to include a setter for the Height property that is bound to the ListBox.ActualHeight property:

    <Style TargetType="{x:Type ListBoxItem}" x:Key="listStyle">
        <Setter Property="Template" Value="{StaticResource listControlTemplate}" />
        <Setter Property="Height" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}, Path=ActualHeight}" />
    </Style>

This will make your items as tall as their container. From there on, you can bind the Grid in the control template to the ActualHeight of it's templated parent and size your image. You will have to play a little bit to achieve the height difference. For example, you can create on row above the row with the images and for the selected image change it's Row and RowSpan properties with a setter.