ConvertBack没被调用?

时间:2014-06-05 05:49:03

标签: wpf binding converter dependency-properties two-way-binding

我在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>

我正在尝试将Visibility绑定到自定义依赖项属性“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时,网格的可见性将通过转换器设置为可见

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函数中设置断点,但从未调用它。

那我该怎么办?

0 个答案:

没有答案