本文介绍了如何绑定可见性属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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代码:

The ViewModel code:

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

IsButtonVisible =false;

有人可以告诉我我要去哪里错吗?

Can anyone tell me where am I going wrong?

推荐答案

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

I'm guessing that the problem is that you are binding to IsButtonVisible on your templated parent. Instead you need to bind to IsButtonVisible on the ViewModel/DataContext of your templated parent - i.e.

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

这篇关于如何绑定可见性属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:27