如何用VisualStateManager隐藏XAML TextBlock?

时间:2014-12-13 14:50:55

标签: xaml visualstatemanager visualstates

我想隐藏基于Visual State页面的TextBlock。我使用VisualStateManager来改变状态良好的状态。但我隐藏TextBlock的代码却不知道。 附加问题:如果两个状态共享相同的指令(如Narrow和Snapped),是否有办法不再写入相同的ObjectAnimationUsingKeyFrames-tags,那么维护的代码量会减少多少?感谢

<TextBlock x:Name="Title" FontSize="20" FontWeight="SemiLight" Margin="12,0,0,1"
                                   Text="{Binding TitleTextBlockText}" VerticalAlignment="Center">
                            <VisualStateManager.VisualStateGroups>
                                <!-- Visual states reflect the application's window size -->
                                <VisualStateGroup>
                                    <VisualState x:Name="DefaultLayout">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility"
                                                                           Storyboard.TargetName="Title">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        Visibility.Visible
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Normal">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility"
                                                                           Storyboard.TargetName="Title">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        Visibility.Visible
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="FullScreenLandscape">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility"
                                                                           Storyboard.TargetName="Title">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        Visibility.Visible
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="FullScreenPortrait">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility"
                                                                           Storyboard.TargetName="Title">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        Visibility.Visible
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Filled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility"
                                                                           Storyboard.TargetName="Title">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        Visibility.Visible
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>

                                    <VisualState x:Name="Snapped">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility"
                                                                           Storyboard.TargetName="Title">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        Visibility.Collapsed
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Narrow">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility"
                                                                           Storyboard.TargetName="Title">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        Visibility.Collapsed
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </TextBlock>

1 个答案:

答案 0 :(得分:0)

如果要执行此操作(例如,在自定义控件的模板中),则应将textbloc包装到网格中。您无法为此类对象设置动画,因为visualstatemanager不能位于将由visualstatemanager本身设置动画的对象内部。 在控件的通用模板中,如下所示:

 <ControlTemplate TargetType="{x:Type local:NumericField}">
          <Grid>
            <VisualStateManager.VisualStateGroups>
              <VisualStateGroup Name="ValidationStates">
                <VisualState Name="Good">
                  <!-- do your things -->
              </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>

            <Border x:Name="ValidationBorderPart"
                    BorderBrush="Black"
                    BorderThickness="1">
              <TextBox x:Name="ValuePart"
                       Foreground="Black"
                       BorderBrush="Transparent"
                       BorderThickness="0"
                       Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:NumericField}},Path=Representation, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
            </Border>

          </Grid>
        </ControlTemplate>