2010-11-08 71 views
0

我有一個WPF網格,其中包含,其中包括一個按鈕。WPF問題與Visibiliy的按鈕時,祖先IsMouseOver

該按鈕默認處於隱藏狀態,只有在鼠標懸停在網格上時才能看到該按鈕。 (功能上,網格是一個標籤頁眉,「消失」按鈕是一個關閉按鈕)。我還重寫了按鈕模板以具有定製的感覺。

現在,當鼠標進入網格時,該按鈕變爲可見,但只要鼠標進入按鈕就會消失。我的直覺是,當鼠標移動到按鈕上時,網格的IsMouseOver變爲False。有沒有解決的辦法?

 <ControlTemplate x:Key="CloseTabButtonTemplate"> 
      <Border Width="14" Height="14" Margin="3" 
        HorizontalAlignment="Right" 
        VerticalAlignment="Center" 

        BorderThickness="1" 
        CornerRadius="2,2,2,2"> 
       <TextBlock Text="x" VerticalAlignment="Center" HorizontalAlignment="Center" 
          FontSize="11" Padding="0" Margin="0,-2,0,0" Foreground="White"/> 
       <Border.Style> 

        <Style TargetType="{x:Type Border}"> 
         <Setter Property="Background" Value="#33DA3030"/> 
         <Setter Property="BorderBrush" Value="White"/> 
         <Setter Property="Visibility" Value="Hidden"/> 
         <Style.Triggers>         
          <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid}},Path=IsMouseOver}" Value="True"> 
           <Setter Property="Visibility" Value="Visible" /> 
          </DataTrigger> 
          <Trigger Property="IsMouseOver" Value="True"> 
           <Setter Property="Background" Value="#FFDA3030"/> 
           <Setter Property="Visibility" Value="Visible" /> 
          </Trigger> 
         </Style.Triggers> 
        </Style> 
       </Border.Style> 
      </Border> 
     </ControlTemplate>   

<Button Grid.Column="2" HorizontalAlignment="Right" Template="{StaticResource CloseTabButtonTemplate}">x</Button> 

謝謝!

回答

2

我看到這裏沒有問題。

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300" 
     > 

    <Window.Resources> 

     <ControlTemplate x:Key="CloseTabButtonTemplate"> 
      .... Your template as in the question .... 
     </ControlTemplate> 

    </Window.Resources> 

    <Grid x:Name="MainGrid" Width="200" Height="200" Background="Transparent"> 

     <Button Template="{StaticResource CloseTabButtonTemplate}" /> 

    </Grid> 
</Window> 

或許父電網缺少Background顏色:我與一個網格一個簡單的窗口進行測試呢?正如你看到我已經讓我的GridTransparent。當我刪除它時,它不會顯示按鈕。

也許再加一點代碼?

+0

謝謝! - 背景=透明固定它。我有興趣瞭解爲什麼容器的背景顏色會影響到這一點。 – 2010-11-08 16:52:54

+0

如果網格沒有背景,它將不會註冊MouseOver,因爲鼠標在沒有背景時不會完全覆蓋網格。將背景設置爲顏色意味着網格具有正文/內容,可以這麼說;) – Arcturus 2010-11-09 09:30:07