在WP7上从它的父ListBox更改绑定的ContentControl的VisualState

时间:2011-08-04 18:36:15

标签: windows-phone-7 silverlight-3.0

我有以下情况:

public class EbookPhotoSummary : ContentControl
{
    ...

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

此ContentControl具有样式定义(我只显示重要部分):

<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>

然后,我把这个ContentControl作为ListBox的DataTemplate:

        <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">
                        <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>

我想要的是当我点击ApplicationBar的按钮时,应用“EbooksList”VisualState,我正试图这样做:

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

    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);
        }
    }

但没有任何反应。我已经知道GoToState返回false,但我不知道为什么。有什么想法吗?

提前致谢。

0 个答案:

没有答案