1

嘿,我在我的项目中创建了一个用户控件。我想当它的 Visibility 属性改变时,运行一行代码

 VisualStateManager.GoToState(this, "Normal", true);

此属性绑定到数据源。

    <usercontrol:CorrectAnswerMessage Grid.Column="1" x:Name="correct"  
Visibility="{Binding IsTrueAnswer,Mode=TwoWay}" VerticalAlignment="Center" 
HorizontalAlignment="Left"></usercontrol:CorrectAnswerMessage>

我正在寻找一种为 Visibility 属性创建事件的方法。当这个属性改变时,事件被触发。

这是我的用户控件

<UserControl
x:Class="Lifener.UserControls.CorrectAnswerMessage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Lifener.UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="500">
<Grid x:Name="CorrectResult" Width="400" Height="350" Visibility="Visible"  HorizontalAlignment="Center" Margin="0">

    <TextBlock Text="&#xE17E;" FontSize="120"  FontFamily="Segoe UI Symbol" Padding="0" Margin="0"     VerticalAlignment="Center" HorizontalAlignment="Center"  />
    <TextBlock  TextAlignment="Center" Text="&#xE17F;" FontSize="60" FontFamily="Segoe UI Symbol" Margin="130,30,55,215" />
    <TextBlock x:Name="Like" TextAlignment="Center" Text="&#xE19F;" FontSize="60" FontFamily="Segoe UI Symbol" Margin="165,30,20,0" Foreground="Green" RenderTransformOrigin="0.5,0.5" Height="104" VerticalAlignment="Top"/>
    <TextBlock x:Uid="AnswerIsRight" HorizontalAlignment="Left" Margin="185,140,0,0"  TextWrapping="Wrap" Text="Hooora, Right. You are awsome." VerticalAlignment="Top" Width="185" Height="140" TextAlignment="Center" FontSize="28" FontWeight="Bold"/>

    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonGroups">
            <VisualState x:Name="Normal" >
                <VisualState.Storyboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Like"
                                                    Storyboard.TargetProperty="Foreground"
                                                    Duration="0:0:2.5"
                                                    AutoReverse="True"
                                                    RepeatBehavior="Forever">

                            <DiscreteObjectKeyFrame Value="Red" KeyTime="0"></DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>

                    </Storyboard>
                </VisualState.Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>


</Grid>

4

1 回答 1

0

如果您不使用 MVVM,那么您可以调用GoToStatesetter for IsTrueAnswer.

使用 MVVM,我不确定最好的方法是什么。也许从用户控件的模型中引发事件VisibilityChanged :

public partial class MyModel
{
    public event EventArgs VisibilityChanged;

    private bool _isTrueAnswer;
    public bool IsTrueAnswer
    {
        get { return _isTrueAnswer; }
        set
        {
            _isTrueAnswer = value;
            VisibilityChanged(this, EventArgs.Empty);
        }
    }

    // etc...
}

并从页面的代码隐藏中添加一个侦听器:

public MyPage()
{
    ((MyModel)MyUserControl.DataContext).VisibilityChanged += (sender, args) => {
        VisualStateManager.GoToState(this, "Normal", true);
    };
}
于 2012-12-03T16:46:44.087 回答