2017-02-01 5 views
0

タッチスクリーンでツールチップの新しい共通クラスが作成されました。そして、私はツールチップのコントロールとページをたくさん持っています。だから私はXAMLでこの共通のクラスについて使いたいだけです。XAMLで共通クラスをバインドする方法

public class CommonLayout : Window 
{ 

    Timer Timer { get; set; } 
    ToolTip toolTip { get; set; } 

    public CommonLayout(TextBlock control) 
    { 
     Timer = new Timer(); 
     Timer.Interval = 3000; 
     Timer.Elapsed += OnTimerElapsed; 

     control.MouseLeave += OnMouseLeave; 
     control.MouseLeftButtonUp += OnMouseLeftButtonUp; 

    } 

    public void OnMouseLeave(object sender, System.Windows.Input.MouseEventArgs e) 
    { 
     CloseToolTip(); 
    } 

    public void OnMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) 
    { 
     toolTip = ((ToolTip)((TextBlock)sender).ToolTip); 
     toolTip.IsOpen = true; 
     Timer.Start(); 
    } 

    private void CloseToolTip() 
    { 
     if (toolTip != null) 
     { 
      toolTip.IsOpen = false; 
     } 
    } 

    private void OnTimerElapsed(object sender, ElapsedEventArgs e) 
    { 
     Timer.Stop(); 
     Application.Current.Dispatcher.BeginInvoke((Action)CloseToolTip, DispatcherPriority.Send); 
    } 

} 

共通クラス@ @XAML 私はのTextBlockの3種類以下についての一般的なツールヒントクラスのバインドにしたいです。 他のページでも使用する必要があります。

<StackPanel Margin="30"> 
     <TextBlock x:Name="textBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="ToolTip Test" 
       ToolTipService.ShowOnDisabled="True" > 
      <TextBlock.ToolTip> 
       <ToolTip Placement="Mouse" Content="This is ToolTip Test." /> 
      </TextBlock.ToolTip> 
     </TextBlock> 

     <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="ToolTip Test1" 
       ToolTipService.ShowOnDisabled="True" > 
      <TextBlock.ToolTip> 
       <ToolTip Placement="Mouse" Content="This is ToolTip Test." /> 
      </TextBlock.ToolTip> 
     </TextBlock> 

     <TextBlock x:Name="textBlock2" HorizontalAlignment="Left" TextWrapping="Wrap" Text="ToolTip Test2" 
       ToolTipService.ShowOnDisabled="True" > 
      <TextBlock.ToolTip> 
       <ToolTip Placement="Mouse" Content="This is ToolTip Test." /> 
      </TextBlock.ToolTip> 
     </TextBlock> 

    </StackPanel> 

答えて

0

CommonLayoutはWindowから継承されています。したがって、Windowsの一部としてではなく、Windowsとしてのみ使用できます。

あなたの目的のためには、行動の仕組みを使用する方がはるかに適しています。これは特にあなたの場合です。

public class CommonLayout : Behavior<UIElement> { 
    // your code with corrections from tutorial 
} 

XAMLは、このようなものです::アセンブリへの参照を追加することを忘れないでください

<TextBlock> 
<e:Interaction.Behaviors> 
    <b:CommonLayout/> 
</e:Interaction.Behaviors> 
</TextBlock> 

、それがどのように見えるだろう結果https://wpftutorial.net/Behaviors.html

:次のチュートリアルに従ってください。前:System.Windows.Interactivity

私はそれがあなたを助けてくれることを願っています。

+0

私は私の答えに追加しました。もう少し考えがありますか? – Song

+0

私は、現在のコメントが規則を破らないとは確信していません。しかし、私の答えが本当に助けになったら、ここに記入してください。 –

関連する問題