2012-10-30 95 views
6

我有一個帶背景圖像的wppf按鈕。禁用按鈕鼠標超過效果

當我將鼠標移到背景上時,它將是空的,並顯示一個按鈕。

如何禁用鼠標效果?

<Button BorderBrush="{x:Null}" Content="Reset" BorderThickness="0"Foreground="White" Focusable="False"> 
         <Button.Background> 
          <ImageBrush ImageSource="button.png" /> 
         </Button.Background> 
+0

聽起來就像使用Visual State Manager的觸發器一樣。請提供所有相關的xaml。 – Aphelion

+0

試試這個; http://stackoverflow.com/questions/1302756/why-is-the-buttons-background-changing pro-tip,如果你沒有評論的評論發佈「trival答案」,看看會發生什麼。 –

回答

13

下面是我如何禁用按鈕上的視覺鼠標懸停效果。我留下了一些我的設置,只是爲了讓你感覺如何玩觸發器和東西,隨時嘗試!

<Style TargetType="Button" x:Key="ImageButton" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Border Name="border" 
         BorderThickness="{TemplateBinding BorderThickness}" 
         Padding="{TemplateBinding Padding}" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         CornerRadius="5" 
         Background="{TemplateBinding Background}"> 
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="BorderBrush" Value="Gainsboro" /> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Opacity" Value="0.25" /> 
         <Setter Property="BorderBrush" Value="Transparent" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

編輯:使用「BasedOn」+設置FocusVisualStyle爲null(前2行)擺脫鼠標的效果。其他一切都只是例子。我在那裏添加了一個邊框,以便通過觸發器進行播放(因爲我想要一個自定義的鼠標懸停效果)。

+2

我不同意這篇文章。前兩行在我的應用程序中沒有影響。我在其他地方讀過,您不允許部分覆蓋該模板,並且您必須爲其生成完整且完整的模板。不幸的是,我還沒有找到一個可行的解決方案,但這篇文章中的建議絕對不適合我。 – shawn1874

+1

@ shawn1874 - 「我讀過其他地方」可以從一兩個鏈接中獲益,以解釋這種方法的潛在問題。你有沒有試過整個樣本,而不是前兩行,在一個空的項目中,「你的應用程序」沒有改變,看看你是否仍然無法工作? 7k觀點,12贊成,並沒有一個downvote,我的印象是,這個答案確實最滿意。 – Joe

8

ButtonControlTemplate中,Background屬性將被觸發器覆蓋。
您可以重做整個ControlTemplate並離開了背景變化或只需添加圖像作爲Button像這樣的內容,以避免所有這些併發症:

<Button.Content> 
    <Image Source="button.png" /> 
</Button.Content> 

或者,如果你需要的文字,有作爲:

<Button.Content> 
    <Grid> 
     <Image Source="button.png" /> 
     <TextBlock Text="Reset" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
    <Grid> 
</Button.Content> 
+0

無法使用它,因爲我有Content =「Reset」。 –

+0

通過使用面板將圖像和文本塊與「重置」結合起來,您仍然可以在其中執行復雜的佈局 – MrDosu

+1

感謝您提供非常簡單的解決方案 –