0

我在 ControlTemplate 中有一个像这样的网格

 <Grid x:Name="quickMsgGrid" Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsQuickMsg, Converter={StaticResource BoolToVisibility}, Mode=TwoWay}">                                    
                                <Border CornerRadius="{Binding ElementName=mainBorder,Path=CornerRadius}" Background="{StaticResource loadingBackgroundBrush}" />
                                <TextBlock x:Name="txtQuickMsg" HorizontalAlignment="Center" VerticalAlignment="Center">asdfasdfasdfasdf</TextBlock>
                            </Grid>

我正在尝试将可见性绑定到自定义依赖属性“IsQuickMsg”

public static readonly DependencyProperty IsQuickMsgProperty = DependencyProperty.Register("IsQuickMsg", typeof(bool), typeof(baseWindow));

    public bool IsQuickMsg
    {
        get { return (bool)GetValue(IsQuickMsgProperty); }
        set { SetValue(IsQuickMsgProperty, value); }
    }

在这种情况下,当我在代码隐藏中将 IsQuickMsg 设置为 true 时,Grid 的 Visibility 将通过转换器设置为 Visible

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((bool)value ? Visibility.Visible : Visibility.Hidden);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((Visibility)value == Visibility.Visible);
    }
}

一旦网格可见,它将开始一个故事板来播放一些动画,然后在完成后将网格的可见性设置为隐藏

<Trigger Property="IsQuickMsg" Value="True">                            
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="quickMsgGrid">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Visible</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                    <DoubleAnimation BeginTime="0:0:0.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="quickMsgGrid" From="0" To="1" Duration="0:0:0.2"/>
                                    <DoubleAnimation BeginTime="0:0:2.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="quickMsgGrid" From="1" To="0" Duration="0:0:0.2"/>
                                    <ObjectAnimationUsingKeyFrames BeginTime="0:0:2.5" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="quickMsgGrid">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Hidden</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>                                    
                            </BeginStoryboard>
                        </Trigger.EnterActions>

但是,当故事板结束时,Grid 的可见性被设置为隐藏,但它不会将 Visibility 值转换回我的自定义依赖属性“IsQuickMsg”

我在 ConvertBack 函数中设置了断点,但它从未被调用过。

那么我在这里做错了什么?

4

0 回答 0