如何绑定可见性属性

时间:2012-05-07 09:14:37

标签: wpf visibility show-hide adorner

XAML代码如下:

<Helper:BooleanVisibilityValueConverter x:Key="BoolToVisible" />
   <Control x:Name="ResizeDecorator" MouseMove="ResizeDecorator_MouseMove" Template=" {StaticResource ResizeDecoratorTemplate}" Visibility="{Binding IsButtonVisible,Converter= {StaticResource ResourceKey=BoolToVisible},RelativeSource={RelativeSource  TemplatedParent}}" />
<Helper:BooleanVisibilityValueConverter x:Key="BoolToVisible" />
<Control x:Name="ResizeDecorator"  MouseMove="ResizeDecorator_MouseMove" Template="{StaticResource ResizeDecoratorTemplate}" Visibility="{Binding IsButtonVisible,Converter={StaticResource ResourceKey=BoolToVisible},RelativeSource={RelativeSource TemplatedParent}}"  />

ViewModel代码:

private bool _IsButtonVisible;
public bool IsButtonVisible
{
    get { return _IsButtonVisible; }
    set
    {
        _IsButtonVisible = value;
        NotifyPropertyChanged("IsButtonVisible");
    }
}

IsButtonVisible =false;

谁能告诉我哪里出错?

1 个答案:

答案 0 :(得分:1)

我猜测问题是你绑定了模板化父级的IsButtonVisible。相反,您需要绑定到模板化父级的ViewModel / DataContext上的IsButtonVisible - 即

Visibility="{Binding DataContext.IsButtonVisible,Converter={StaticResource ResourceKey=BoolToVisible},RelativeSource={RelativeSource TemplatedParent}}"