PropertyChanged事件不使用转换器更改用于绑定的UI

时间:2012-04-02 18:50:50

标签: wpf

我的xaml低于“canvas”...我想改变其可见性取决于属性变化。

XAML:

<d1:BooleanToVisibilityConverter x:Key="BoolToVis" />

Visibility="{Binding Path=IsVisible, Converter={StaticResource BoolToVis}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

我在ViewModel中有这个Visibility属性

private bool _isVisible;
    public bool IsVisible
    {
        get { return _isVisible; }
        set
        {
            _isVisible = value;
            RaisePropertyChanged("IsVisible");
        }
    }

这是我的转换器代码:

class BooleanToVisibilityConverter : IValueConverter
{
    public BooleanToVisibilityConverter() { }
    #endregion

    #region Properties
    public bool Collapse { get; set; }
    #endregion

    #region IValueConverter Members
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool bValue = (bool)value;
        if (bValue)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
            //if (Collapse)
            //    return Visibility.Collapsed;
            //else
            //    return Visibility.Hidden;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //Visibility visibility = (Visibility)value;

        //if (visibility == Visibility.Visible)
        //    return true;
        //else
        //    return false;
        return value;
    }
    #endregion
}

调试时我观察到我的Setter正在改变属性值,但它没有在我的转换器上达到断点。转换器最初只在加载窗口时被击中。之后,即使属性发生变化,它也不会被触发。

1 个答案:

答案 0 :(得分:0)

我读到你正在使用Prism,所以尝试以这种形式更改你的RaisePropertyChanged:

RaisePropertyChanged(() => IsVisible);