views:

117

answers:

1

I guess the following picture depicts the problem better than texts...

alt text

That is what I need.

<ListBox x:Name="NamesListBox" ItemsSource="{Binding}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel x:Name="ItemWrapPanel">
                <WrapPanel.RenderTransform>
                    <TranslateTransform x:Name="ItemWrapPanelTransformation" X="0" />
                </WrapPanel.RenderTransform>
                <WrapPanel.Triggers>
                    <EventTrigger RoutedEvent="WrapPanel.Loaded">
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="ItemWrapPanelTransformation" Storyboard.TargetProperty="X" To="-1000" From="{Binding ElementName=ScrollingListItemsWindow, Path=Width}"  Duration="0:0:9" RepeatBehavior="100" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </WrapPanel.Triggers>
            </WrapPanel>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Label Content="{Binding}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

is what I did. But here I don't wanna hard code -1000 for the X value, rather I want to determine this based on the length of the wrap panel/number of items. Can somebody help me with this ??

Reason why I choose list box is that the number of items can increase and decrease at any time and suits the problem better.

If you have got any other idea, please suggest it too.

Thanks.

+1  A: 

Have you tried to bind X to the WrapPanel, while using a converter ?

The converter will get the WrapPanel instance as a parameter and you can then analyze its properties and return a value based on the width or number of items.

Timores
But I am getting the Width as Nan as it is set to Auto by WPF when I use it as a ItemsHost. Also ActualWidth is always 0.0. Any idea why?? (But your idea of using the converter is cool. Dunno how I missed it)
sudarsanyes
I guess it's because the items are not yet in the WrapPanel when the converter is called. Your sample starts the storyboard when it is loaded; is that your final code ? or is the final code bound to the mouse hover event ? It would be interesting to see if the converter is called later in that case.
Timores
Even if it is on mouse enter, the convert method is called during the load sequence. Any idea why ??
sudarsanyes
I guess the whole WrapPanel definition is "instantiated" when the ListBox needs it. Is there a way to have a property that represents the needed value of X in your DataContext ? This way, you could compute it and update it when needed, bind the animation to it and use INotifyPropertyChanged to have the binding reevaluate the animation property.
Timores