BooleanToHiddenVisibility方法未被执行

时间:2014-08-20 15:07:08

标签: c# wpf xaml

我在MainWindow.xaml中的XAML:

<Window.Resources>
  <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>

<r:RibbonGroup x:Name="LockDataRibbonGroup" Header="Data Editing" Visibility="{Binding   IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}">

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

        return value;
    }
    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Visibility visibility = (Visibility) value;
        if (visibility == Visibility.Visible)
            return true;
        else
            return false;

    }
}

查看型号:

private bool _isVisible = true;
public bool IsVisible
{
    get { return _isVisible; }
    set { _isVisible = value; }
}

if(somecondition)
    IsVisible = false;

没有用。当我在Conver和ConvertBack上放置断点时,它们永远不会被击中。我错过了什么?这与可见性有关。我的BooleanToHiddenVisibility类位于Helpers文件夹中。我的MainWindow.xaml位于View文件夹中。 感谢

2 个答案:

答案 0 :(得分:3)

您已将类声明为“class BooleanToHiddenVisibility:IValueConverter”,但您已使用内部“BooleanToVisibilityConverter”。

替换下面:

<Window.Resources>
  <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>

有了这个:

<Window.Resources>
  <BooleanToHiddenVisibility x:Key="BooleanToHiddenVisibility " />
</Window.Resources>

使用x:Key =“BooleanToHiddenVisibility”作为XAML中的键和当前实现INotifyProipertyChanged

答案 1 :(得分:0)

您忘了表明酒店已更改:

private bool _isVisible = true;
public bool IsVisible
{
    get { return _isVisible; }
    set
    {
        if (value != _isVisible)
        {
            _isVisible = value; 

            this.RaisePropertyChanged("IsVisible");
        }
    }
}

RaisePropertyChanged是从INotifyPropertyChanged提出PropertyChanged事件的方法