如何将WPF中的Expander的样式转换为Silverlight

时间:2011-06-07 20:23:31

标签: silverlight templates coding-style controltemplate

我在WPF中有以下xaml样式,我想将它移植到silverlight。 我遇到了Silverlight中不存在的ControlTemplate触发器问题。 我正在尝试Interaction.Triggers,但它无法正常工作。 如何将ControlTemplate触发器转换为类似Silverlight的东西?

<Style x:Key="ImageExpander" TargetType="Expander">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="Expander">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Name="ContentRow" Height="0"/>
                </Grid.RowDefinitions>
                <Border Name="Border" Grid.Row="0">
                    <Border.Background>
                        <ImageBrush ImageSource="image.png" Stretch="Fill" />
                    </Border.Background>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="20" />
                        </Grid.ColumnDefinitions>
                        <ContentPresenter Grid.Column="0" Margin="4" ContentSource="Header" RecognizesAccessKey="True"></ContentPresenter>
                        <ToggleButton Grid.Column="1" IsChecked="{Binding Path=IsExpanded,Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" OverridesDefaultStyle="True" Template="{StaticResource ImageExpanderToggleButton}"/>
                    </Grid>
                </Border>
                <Border Name="Content" Grid.Row="1">
                    <ContentPresenter Margin="0" />
                </Border>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsExpanded" Value="True">
                    <Setter TargetName="ContentRow" Property="Height" Value="{Binding ElementName=Content,Path=DesiredHeight}" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>

提前致谢。

1 个答案:

答案 0 :(得分:0)

一般来说,WPF中属性触发器的概念通常被Silverlight中的视觉状态所取代。对于Expander.IsExpanded属性,这对应于Expanded visual state。

因此,在您的模板中,您需要以下内容:

<Grid>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="ExpansionStates">
            <VisualState x:Name="Collapsed"/>
            <VisualState x:Name="Expanded">
                <Storyboard>
                    ... (some animation)
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    ...
 </Grid>