2016-04-13 7 views
0

私はユーザーがポップアップを表示したい要素の上にマウスを置くとボタンがあります。 このポップアップの内部には、ユーザーがクリックできるボタンがあります。 すべてがItemscontrolの中にあります。ポップアップを持つボタン

私はすでに、ユーザーがボタンの上にマウスを置いたときにポップアップを表示していますが、私はどのように知らない:ユーザーがポップアップに「フォーカス」していない場合、ポップアップを隠す

1)。

<Button x:Name="MyButton" MouseEnter="LabelShift_MouseDown" Background="{x:Null}" BorderBrush="{x:Null}" Style="{DynamicResource SquareButtonStyle}" > 
     <Grid MaxHeight="80"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="3*"/> 
       <RowDefinition Height="2*"/> 
      </Grid.RowDefinitions> 
      <Viewbox Grid.Row="0" > 
       <TextBlock Name="Identifier" FontSize="26" Margin="10,10,10,10" Foreground="White" Text="{Binding Identifier}"/> 
      </Viewbox> 
      <Viewbox Grid.Row="1" VerticalAlignment="Bottom"> 
       <TextBlock Name="Value" FontSize="24" Margin="10,10,10,10" Foreground="White" Text="{Binding Total, StringFormat='C'}"/> 
      </Viewbox> 
     </Grid> 
     <Button.Triggers> 
      <EventTrigger RoutedEvent="MouseEnter"> 

       <BeginStoryboard> 
        <Storyboard> 
         <BooleanAnimationUsingKeyFrames 
            Storyboard.TargetName="ToolTip" 
            Storyboard.TargetProperty="IsOpen"> 
          <DiscreteBooleanKeyFrame 
             KeyTime="00:00:00" 
             Value="True" /> 
         </BooleanAnimationUsingKeyFrames> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger> 
      <EventTrigger RoutedEvent="MouseLeave"> 

       <BeginStoryboard> 
        <Storyboard> 
         <BooleanAnimationUsingKeyFrames 
            Storyboard.TargetName="ToolTip" 
            Storyboard.TargetProperty="IsOpen"> 
          <DiscreteBooleanKeyFrame 
             KeyTime="00:00:00" 
             Value="False" /> 
         </BooleanAnimationUsingKeyFrames> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger> 

     </Button.Triggers> 
    </Button> 


<Popup x:Name="ToolTip" PlacementTarget="{Binding ElementName=MyButton}" StaysOpen="True" Placement="Bottom" Height="Auto" Width="{Binding ActualWidth, ElementName=MyButton}" AllowsTransparency="True"> 
    <Grid> 
     <Border BorderBrush="#909090" BorderThickness="1" CornerRadius="1" > 
      <StackPanel Margin="10"> 
       <!-- Content/Elements--> 
      </StackPanel> 
     </Border> 
    </Grid> 
<Popup.Triggers> 
    <EventTrigger RoutedEvent="MouseEnter"> 

     <BeginStoryboard> 
      <Storyboard> 
       <BooleanAnimationUsingKeyFrames 
                    Storyboard.TargetName="ToolTip" 
                    Storyboard.TargetProperty="IsOpen"> 
        <DiscreteBooleanKeyFrame 
                     KeyTime="00:00:00" 
                     Value="True" /> 
       </BooleanAnimationUsingKeyFrames> 
      </Storyboard> 
     </BeginStoryboard> 
    </EventTrigger> 

</Popup.Triggers> 

+0

標準的なツールヒントがある場合は、このカスタムマウス入力のツールヒントは適切ではありません – Paparazzi

答えて

0

ここでどのように達成するかの簡単な例です:ポップアップ上のユーザの焦点は、ボタンに焦点を当てた後、ポップアップが今

私のコードを開いたままにしなければならない場合

2)ドロップダウン機能私がこれを共有している理由は、以下の理由によるものです。

a)ToggleButtonを継承し、必要なすべての機能を提供します。

b)リトルコード。

c)動作します。

using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Controls.Primitives; 
using System.Windows.Data; 

namespace Controls 
{ 
    public class DropDownButton : System.Windows.Controls.Primitives.ToggleButton 
    { 
     public static readonly DependencyProperty DropDownProperty = DependencyProperty.Register("DropDown", typeof(ContextMenu), typeof(DropDownButton), new UIPropertyMetadata(null)); 
     public ContextMenu DropDown 
     { 
      get 
      { 
       return (ContextMenu)GetValue(DropDownProperty); 
      } 
      set 
      { 
       SetValue(DropDownProperty, value); 
      } 
     } 

     public DropDownButton() 
     { 
      // Bind the ToogleButton.IsChecked property to the drop-down's IsOpen property 
      Binding binding = new Binding("DropDown.IsOpen"); 
      binding.Source = this; 
      this.SetBinding(IsCheckedProperty, binding); 
     } 

     protected override void OnClick() 
     { 
      if (DropDown != null) 
      { 
       //If there is a drop-down assigned to this button, then position and display it 
       DropDown.PlacementTarget = this; 
       DropDown.Placement = PlacementMode.Bottom; 
       DropDown.IsOpen = true; 
      } 
     } 
    } 
} 

あなたがたContextMenuの代わりに、ユーザーコントロールを使用したい場合は、単純に新しいプロパティを追加することができると言った制御ユーザーコントロールのみを表示するようにバインディングを使用することができ、のIsOpenと呼ばれ、そして、サンプル上記と同じロジックを次ToggleButtonがチェックされたとき。

+0

ユーザーがドロップダウンを開いてボタンをクリックすると、この動作はどのようになりますか?私は 'ToggleButton'がそのように動作していることがわかったので、単純な' OpenCommand'にバインドされた通常のボタンを使い、 'IsOpen'中にボタンを無効にしました。 –

+0

あなたはバインディングを削除したいでしょう。次に、クリックすると、開いている場合は閉じます。閉じている場合は開きます。 'if(DropDown!= null && DropDown.IsOpen)DropDown.IsOpen = false; else if(DropDown!= null&DropDown.IsClosed)DropDown.IsOpen = true; '。 ToggleButtonがチェックされているかどうかをチェックすることもオプションです。 –

+0

これで、ビジュアルにボタンを使用するだけです。 –

関連する問題