Использование ObjectAnimation для изменения изображения ToggleButton в зависимости от CheckState - PullRequest
1 голос
/ 13 апреля 2011

У меня есть код ниже, который должен изменить изображение при проверке ToggleButton, но, к сожалению, он ничего не делает.Есть идеи?

<Style x:Key="CustomButton" TargetType="ToggleButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                    <Grid>
                        <Ellipse HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="CheckedImage" Visibility="Collapsed" >
                                <Ellipse.Fill>
                                    <ImageBrush ImageSource="Checked.png" Stretch="UniformToFill"></ImageBrush>
                                </Ellipse.Fill>
                            </Ellipse>
                            <Ellipse HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="UnCheckedImage" Visibility="Collapsed">
                                <Ellipse.Fill>
                                    <ImageBrush ImageSource="UnChecked.png" Stretch="UniformToFill"></ImageBrush>
                                </Ellipse.Fill>
                        </Ellipse>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CheckStates">
                                <VisualState x:Name="Checked">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckedImage" Storyboard.TargetProperty="Visibility" Duration="0">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="UnChecked">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="UnCheckedImage" Storyboard.TargetProperty="Visibility" Duration="0">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Cursor" Value="Hand" />
    </Style>

Ответы [ 2 ]

0 голосов
/ 13 апреля 2011

отредактировал для вас:

<Style x:Key="CustomButton" TargetType="ToggleButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CheckStates">
                                <VisualState x:Name="Checked">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="CheckedImage">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="UnCheckedImage">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Collapsed</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="UnChecked">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="CheckedImage">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Collapsed</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="UnCheckedImage">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unchecked"/>
                                <VisualState x:Name="Indeterminate"/>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="CommonStates" ei:ExtendedVisualStateManager.UseFluidLayout="True">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed"/>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <VisualStateManager.CustomVisualStateManager>
                            <ei:ExtendedVisualStateManager/>
                        </VisualStateManager.CustomVisualStateManager>
                        <Ellipse HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="CheckedImage" Visibility="Collapsed" >
                                <Ellipse.Fill>
                                    <ImageBrush ImageSource="Checked.png" Stretch="UniformToFill"/>
                                </Ellipse.Fill>
                            </Ellipse>
                            <Ellipse HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="UnCheckedImage">
                                <Ellipse.Fill>
                                    <ImageBrush ImageSource="UnChecked.png" Stretch="UniformToFill"/>
                                </Ellipse.Fill>
                        </Ellipse>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Cursor" Value="Hand" />
    </Style>
0 голосов
/ 13 апреля 2011

Попробуйте изменить DiscreteObjectKeyFrame на: -

  <DiscreteObjectKeyFrame KeyTime="0:0:0">
    <DiscreteObjectKeyFrame.Value>
      <Visibility>Collapsed</Visibility>
    </DiscreteObjectKeyFrame.Value>
  </DiscreteObjectKeyFrame>

Я обнаружил, что синтаксический анализатор не работает, когда ключевое время не использует формат временного интервала.Также XamlParser не знает, что значение имеет тип Visibility, поэтому вам нужно быть явным.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...