开发者

Change binded ContentControl's VisualState from it's parent ListBox on WP7

I have the following situation:

public class EbookPhotoSummary : ContentControl
{
    ...

    #region Construtores
    public EbookPhotoSummary()
    {
        DefaultStyleKey = typeof(EbookPhotoSummary);
        this.Loaded += new RoutedEventHandler(EbookPhotoSummary_Loaded);
    }
    ...
}

This ContentControl has a Style definition (I'm only displaying the important part):

<Style TargetType="local:EbookPhotoSummary">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:EbookPhotoSummary">
                <Border>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="EbookListingStates">
                            <VisualState x:Name="EbooksThumbnail"/>
                            <VisualState x:Name="EbooksList">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content" Storyboard.TargetName="hplMore">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="[link]"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid x:Name="LayoutRoot" Height="235">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="140"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <ContentControl Content="{Binding CoverImage}" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="0"/>
                        <Grid Margin="10,0,0,0" Grid.Column="1">
                            ...
                            <HyperlinkButton Name="hplMore" Content="{Binding Path=LocalizedResources.Button_Text_More_Library, Source={StaticResource LocalizedStrings}}" Foreground="{StaticResource PhoneAccentBrush}" Height="30" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Row="3"/>
                        </Grid>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Then, I put this ContentControl as DataTemplate of a ListBox:

        <ListBox x:Name="lsbEbooksInLibrary" ItemsSource="{Binding}">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border Background="Transparent">
       开发者_JAVA技巧                 <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu>
                                <toolkit:MenuItem Header="Add Color" Click="ContextMenuItem_Click"/>
                                <toolkit:MenuItem Header="Remove Color" Click="ContextMenuItem_Click"/>
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                        <local:EbookPhotoSummary x:Name="ebookTeste" Margin="0,0,0,10">
                            ...
                        </local:EbookPhotoSummary>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

What I want is when I click an ApplicationBar's button, the "EbooksList" VisualState be applied, which I'm trying to do this way:

    private void mniVisualizationMode_Click(object sender, EventArgs e)
    {
        this.GetItemsRecursive(lsbEbooksInLibrary);
        lsbEbooksInLibrary.UpdateLayout();
    }

and

    private void GetItemsRecursive(DependencyObject lb)
    {
        var childrenCount = VisualTreeHelper.GetChildrenCount(lb);

        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(lb, i);

            if (child is ListBoxItem)
            {
                VisualStateManager.GoToState(child as Control, "EbooksList", false);
            }

            GetItemsRecursive(child);
        }
    }

but nothing happens. I already know that GoToState is returning false, but I don't know why. Any ideas?

Thanks in advance.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜