绑定错误关于ToolTip可见性

时间:2015-02-10 08:48:17

标签: c# wpf binding

我正在尝试将ToolTip的visibility属性绑定到Button的IsEnabled

 <Button Name="bbb" Content="Train" Command="{Binding TrainCmd}" ToolTipService.ShowOnDisabled="True">
      <Button.ToolTip>
          <ToolTip Content="{Binding TrainToolTip}" Visibility="{Binding IsEnabled, Converter={StaticResource InverseBooleanToVisibilityConverter}, ElementName=aaa}">
          </ToolTip>
      </Button.ToolTip>
 </Button> 

但是我收到以下错误:

  

“System.Windows.Data错误:4:无法找到绑定源   引用'ElementName = aaa'。 BindingExpression:路径=的IsEnabled;   的DataItem = NULL; target元素是'ToolTip'(Name ='');目标财产   是'可见性'(类型'可见性')“

我也试过

 <Button Content="Train" Command="{Binding TrainCmd}" ToolTipService.ShowOnDisabled="True">
    <Button.ToolTip>
        <ToolTip Content="{Binding TrainToolTip}" Visibility="{Binding IsEnabled, Converter={StaticResource InverseBooleanToVisibilityConverter}, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}}"/>
    </Button.ToolTip>
 </Button>

然后我得到了

  

“System.Windows.Data错误:4:无法找到绑定源   参考'RelativeSource FindAncestor,   AncestorType ='System.Windows.Controls.Button',AncestorLevel ='1''。   BindingExpression:路径=的IsEnabled;的DataItem = NULL;目标元素是   'ToolTip'(Name =''); target属性是'Visibility'(类型   '可见性')“

1 个答案:

答案 0 :(得分:2)

ToolTip与PlacementTarget不在同一个可视树中。

 <Button Name="bbb" Content="Train" Command="{Binding TrainCmd}" ToolTipService.ShowOnDisabled="True">
      <Button.ToolTip>
           <ToolTip Content="{Binding TrainToolTip}" Visibility="{Binding PlacementTarget.IsEnabled, Converter={StaticResource InverseBooleanToVisibilityConverter}, RelativeSource={RelativeSource Self}">
            </ToolTip>
       </Button.ToolTip>
 </Button>