2017-04-10 56 views
-1

我剛剛在WPF窗口中放置了一個完全正常的默認按鈕,期望它具有一些正常的按鈕狀態。但它只是對鼠標作出反應,而不是鼠標向下。我只需要它有兩個狀態,所以我可以做到沒有超過狀態。我如何讓它對被按下的鼠標按鈕做出反應?如何使按鈕在WPF中處於停止狀態而不是處於停止狀態?

這裏是我的按鈕:

<Button Width="380" 
     Height="80" 
     FontSize="30" 
     FontFamily="VAG Rounded" 
     Background="#337A05" 
     Foreground="White" 
     Margin="0,15" 
     Content="BACK TO GAMES" /> 
+0

爲什麼downvote? –

回答

1

您可以在Button在設計模式在Visual Studio中右鍵單擊並選擇編輯模板 - >編輯複製,然後修改默認控件模板,按您的要求,例如通過去除IsMouseOver觸發和改變的值在Background屬性setter在IsPressed觸發:

<Window...> 
<Window.Resources> 
<Style x:Key="FocusVisual"> 
    <Setter Property="Control.Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
<SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/> 
<SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/> 
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/> 
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/> 
<SolidColorBrush x:Key="Button.Pressed.Background" Color="Red"/> 
<SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/> 
<SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/> 
<SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/> 
<SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/> 
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"> 
    <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/> 
    <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/> 
    <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Padding" Value="1"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> 
        <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsDefaulted" Value="true"> 
         <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
        </Trigger> 
        <!--<Trigger Property="IsMouseOver" Value="true"> 
           <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/> 
           <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/> 
          </Trigger>--> 
        <Trigger Property="IsPressed" Value="true"> 
         <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/> 
         <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/> 
         <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/> 
         <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
</Window.Resources> 
<Grid> 
    <Button Width="380" 
    Height="80" 
    FontSize="30" 
    FontFamily="VAG Rounded" 
    Background="#337A05" 
    Foreground="White" 
    Margin="0,15" 
    Content="BACK TO GAMES" 
    Style="{StaticResource ButtonStyle1}"/> 
</Grid> 
</Window> 

沒有,沒有,你可以簡單的設置要做到這一點:)你必須替換模板屬性。

+0

好主。這很不方便。我認爲功能按鈕不會是一個用戶友好的IDE,它會讓用戶的工作如此艱難。我希望像這樣的功能不會佔用更多的XAML,而不是我的窗口其餘部分的總和。 –

+0

您可以將樣式放入您從App.xaml合併的ResourceDictionary中。然後,您將能夠從應用程序中的所有視圖中引用它。 – mm8

+0

如果您的問題已解決,請記住接受答案:https://meta.stackexchange.com/questions/23138/how-to-accept-the-answer-on-stack-overflow – mm8

相關問題