ToggleButton上的MouseOver弹出文本

时间:2013-11-14 11:46:11

标签: wpf

我编码在MouseOver ToggleButton时弹出文字。我也得到了它,但真正的问题是弹出的文本不是保持不变,即它在ToggleButton上不断摇晃。还有一件事是弹出的文本出现在ToggleButton本身上,但它应该在它之下。我怎么能摆脱这个呢?

这是我的代码看起来

<ToggleButton x:Name="btn" Width="20" Height="15">
    <Image Source="../Images/flag_orange.ico"/>
</ToggleButton>
<Popup x:Name="popUp" IsOpen="{Binding IsChecked, ElementName=btn, Mode=TwoWay}"
       StaysOpen="False" PlacementTarget="{Binding ElementName=btn}"
       Placement="Bottom" PopupAnimation="Slide" HorizontalOffset="-5"
       VerticalOffset="3">
    <Border Background="DarkGray">
        <TextBox Text="Its a place holder for user notes" x:Name="tbText"/>                         
    </Border>                    
</Popup>
<TextBlock x:Name="tbTextBlock" 
           Visibility="{Binding Path=IsMouseOver,ElementName=btn,Mode=OneWay,
                        Converter={StaticResource BoolToVisibilityConverter}}"
           Text="{Binding ElementName=tbText, Path=Text, Mode=TwoWay}" />

1 个答案:

答案 0 :(得分:0)

在TextBlock上使用IsHitTestVisible="False"。鼠标悬停已被Textblock捕获,因此当ToggleButton出现时,您的鼠标不会超过TextBlock

要在ToggleButton下方获取TextBlock,请使用Margin="0,15,0,0"StackPanel按顺序获取这些控件:

<StackPanel>
  <ToggleButton x:Name="btn" Width="20" Height="15">
    <Image Source="../Images/flag_orange.ico"/>
  </ToggleButton>
  <Popup x:Name="popUp" IsOpen="{Binding IsChecked, ElementName=btn, Mode=TwoWay}"
         StaysOpen="False" PlacementTarget="{Binding ElementName=btn}"
         Placement="Bottom" PopupAnimation="Slide" HorizontalOffset="-5"
         VerticalOffset="3">
    <Border Background="DarkGray">
      <TextBox Text="Its a place holder for user notes" x:Name="tbText"/>
    </Border>
  </Popup>
  <TextBlock x:Name="tbTextBlock" 
         IsHitTestVisible="False"
         Visibility="{Binding Path=IsMouseOver,ElementName=btn,Mode=OneWay,
                      Converter={StaticResource BoolToVisibilityConverter}}"
         Text="{Binding ElementName=tbText, Path=Text, Mode=TwoWay}" />
</StackPanel>