2010-08-19 81 views
5

更新:使用Expression Blend的3WPF按鈕isPressed和的IsEnabled問題

我想要的風格IsPressed &的IsEnabled(假)屬性觸發了在WPF應用程序類的按鈕。

下面是使用樣式的按鈕用戶控件...

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    x:Class="Kiosk.ButtonTest" 
    x:Name="UserControl"> 

    <Grid x:Name="LayoutRoot"> 
     <Button HorizontalAlignment="Left" Style="{DynamicResource BlueButton}" VerticalAlignment="Top" Width="155" Content="Button" Height="52.9"/> 
    </Grid> 
</UserControl> 

而這裏的風情片段...

<!-- Blue Button --> 
    <Style x:Key="BlueButton" TargetType="{x:Type Button}"> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Background" Value="{DynamicResource BlueGradient3}"/> 
     </Trigger> 
     <Trigger Property="IsEnabled" Value="False"> 
      <Setter Property="Background" Value="{DynamicResource DarkGradient1}"/> 
      <Setter Property="BorderBrush" Value="{DynamicResource BlueGradient3}"/> 
     </Trigger> 
     <Trigger Property="IsPressed" Value="True"> 
      <Setter Property="Background" Value="{DynamicResource DarkGradient1}"/> 
      <Setter Property="BorderBrush" Value="{DynamicResource BlueGradient1}"/> 
     </Trigger> 
    </Style.Triggers> 
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> 
    <Setter Property="Background" Value="{DynamicResource BlueGradient1}"/> 
    <Setter Property="BorderBrush" Value="{DynamicResource BlueGradient2}"/> 
    <Setter Property="Foreground" Value="White"/> 
    <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="TextBox.TextAlignment" Value="Center"/>  
    <Setter Property="FontFamily" Value="Trebuchet MS"/> 
    <Setter Property="FontSize" Value="18"/> 
    <Setter Property="Effect" Value="{DynamicResource KioskStandardDropShadow}" /> 
</Style> 
<LinearGradientBrush x:Key="BlueGradient1" EndPoint="0.5,1" StartPoint="0.5,0"> 
    <GradientStop Color="#FF3FA2FD" Offset="0"/> 
    <GradientStop Color="#FF014782" Offset="1"/> 
</LinearGradientBrush> 
<LinearGradientBrush x:Key="BlueGradient2" EndPoint="0.5,1" StartPoint="0.5,0"> 
    <GradientStop Color="#FF014782" Offset="0"/> 
    <GradientStop Color="#FF3FA2FD" Offset="1"/> 
</LinearGradientBrush> 
<LinearGradientBrush x:Key="BlueGradient3" EndPoint="0.5,1" StartPoint="0.5,0"> 
    <GradientStop Color="#FF014782" Offset="1"/> 
    <GradientStop Color="#FF0B2135" Offset="0"/> 
</LinearGradientBrush> 
<LinearGradientBrush x:Key="DarkGradient1" EndPoint="0.5,1" StartPoint="0.5,0"> 
    <GradientStop Color="#FF2A2A2A" Offset="0"/> 
    <GradientStop Color="#FF474747" Offset="0.478"/> 
    <GradientStop Color="#FF323232" Offset="0.487"/> 
    <GradientStop Color="Black" Offset="1"/> 
    <GradientStop Color="#FF282828" Offset="0.681"/> 
</LinearGradientBrush> 
    <!-- Regular Drop Shadow --> 
    <DropShadowEffect x:Key="KioskStandardDropShadow" Opacity="0.6" BlurRadius="10" ShadowDepth="5" Direction="308"/> 
<!-- fragment end --> 

默認和鼠標懸停改變做工精細,但假的IsEnabled並且isPressed仍然顯示Button默認的顏色。

我在做什麼錯?

+0

我試過你的代碼,它適用於我:default-light blue,mouseover-dark blue,IsPressed-Gray,Disabled - Gray。但是我做了FocusVisualStyle的setter,因爲你沒有包含它。 – dnr3 2010-08-19 03:12:39

+0

這不是一個答案,我知道,但嘗試視覺狀態管理器。 – 2010-08-19 05:33:35

+1

您可以編輯您的問題以添加額外的代碼,而不是將其作爲評論發佈。 – ChrisF 2010-08-19 16:03:56

回答

7

我固定它在http://mark-dot-net.blogspot.com/2007/07/creating-custom-wpf-button-template-in.html研究代碼後...

這就是我最終的結果,這很好。

<!-- Blue Button --> 
<Style x:Key="BlueButton" TargetType="{x:Type Button}"> 
<Setter Property="OverridesDefaultStyle" Value="True"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Border Name="border" 
        BorderThickness="2" 
        Padding="4,2" 
        BorderBrush="{DynamicResource BlueGradient2}" 
        CornerRadius="5" 
        Background="{TemplateBinding Background}"> 
        <Grid > 
        <ContentPresenter 
           HorizontalAlignment="Center" 
           VerticalAlignment="Center" 
           Name="content"/> 
        </Grid> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="Background" Value="{DynamicResource BlueGradient3}"/> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter Property="Background" Value="{DynamicResource DarkGradient1}"/> 
         <Setter Property="BorderBrush" Value="{DynamicResource BlueGradient3}"/> 
        </Trigger> 
        <Trigger Property="IsPressed" Value="True"> 
         <Setter Property="Background" Value="{DynamicResource DarkGradient1}"/> 
         <Setter Property="BorderBrush" Value="{DynamicResource BlueGradient1}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="SnapsToDevicePixels" Value="true"/> 
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> 
    <Setter Property="Background" Value="{DynamicResource BlueGradient1}"/> 
    <Setter Property="BorderBrush" Value="{DynamicResource BlueGradient2}"/> 
    <Setter Property="Foreground" Value="White"/> 
    <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="TextBox.TextAlignment" Value="Center"/>  
    <Setter Property="FontFamily" Value="Trebuchet MS"/> 
    <Setter Property="FontSize" Value="15pt"/> 
    <Setter Property="Effect" Value="{DynamicResource KioskStandardDropShadow}" /> 
</Style> 
0

您需要替換ControlTemplate以更改按鈕的背景顏色。

這是我從MSDN複製而來的,與您的代碼一起使用的很好。
您可以將您的特定覆蓋合併到此樣式。

編輯: 做出如下的工作作風,爲的是,你需要從微軟下載Styling with Control Templates Sample。如果從示例中包含Button.xaml和Shared.xaml,則以下樣式應該可以工作,因爲這兩個文件包含下面列出的所有StaticResoruces XAML。我在Visual Studio 2008測試

這裏是我chanaged用戶conttrol:

<UserControl x:Class="ButtonPressed.Views.KioskButton" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    Height="300" Width="300"> 

    <UserControl.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary 
       Source="Button.xaml"> 
      </ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
    </UserControl.Resources> 

    <Grid> 
    <Button HorizontalAlignment="Left" VerticalAlignment="Top" Width="155" Content="Button" Height="52.9"/> 
    </Grid> 
</UserControl> 

這裏是Button.xaml的按鈕樣式的一部分:

<Style TargetType="Button"> 
    <Setter Property="SnapsToDevicePixels" Value="true"/> 
    <Setter Property="OverridesDefaultStyle" Value="true"/> 
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> 
    <Setter Property="MinHeight" Value="23"/> 
    <Setter Property="MinWidth" Value="75"/> 
    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="Button"> 
     <Border 
      x:Name="Border" 
      CornerRadius="2" 
      BorderThickness="1" 
      Background="{StaticResource NormalBrush}" 
      BorderBrush="{StaticResource NormalBorderBrush}"> 
      <ContentPresenter 
      Margin="2" 
      HorizontalAlignment="Center" 
      VerticalAlignment="Center" 
      RecognizesAccessKey="True"/> 
     </Border> 
     <ControlTemplate.Triggers> 
      <Trigger Property="IsKeyboardFocused" Value="true"> 
      <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DefaultedBorderBrush}" /> 
      </Trigger> 
      <Trigger Property="IsDefaulted" Value="true"> 
      <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DefaultedBorderBrush}" /> 
      </Trigger> 
      <Trigger Property="IsMouseOver" Value="true"> 
      <Setter TargetName="Border" Property="Background" Value="{StaticResource DarkBrush}" /> 
      </Trigger> 
      <Trigger Property="IsPressed" Value="true"> 
      <Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}" /> 
      <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource PressedBorderBrush}" /> 
      </Trigger> 
      <Trigger Property="IsEnabled" Value="false"> 
      <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" /> 
      <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" /> 
      <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/> 
      </Trigger> 
     </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
</Style> 
+0

將此作爲樣式(使用x:Key =「CustomButton」來引用它)添加到Expression Blend中,只會給出「無效的XA​​ML」錯誤。 – ocodo 2010-08-19 23:54:50

+0

我添加了哪裏可以找到StaticResources的參考。 – Zamboni 2010-08-20 02:57:23

+0

不幸的是,ControlTemplates樣式不適用於Expression Blend 3. 但是沒關係,我添加了一個適用於它的答案。 – ocodo 2010-08-20 06:01:32