2012-03-27 102 views
0

我照着這個例子做了一個自定義的按鈕suface:如何設置高級樣式定製表面按鈕

<Button> 
    <Button.Template> 
     <ControlTemplate TargetType="Button"> 
      <Grid> 
       <!-- my custom shape --> 
            <Polygon 
            Points="400,150 100,350 100,350, 500,350" 
            Stroke="Purple" 
            StrokeThickness="2" 
            Name="L1"> 
             <Polygon.Fill> 
              <SolidColorBrush Color="Blue" Opacity="0.4"/> 
             </Polygon.Fill> 
        </Polygon> 
       <ContentPresenter /> 
      </Grid> 
     </ControlTemplate> 
    </Button.Template> 
</Button> 

但與風格的一個問題 - 沒有觸摸時/ holded者/發佈行爲。你能給我一個線索怎麼做嗎?

感謝

回答

1

一個例子對照模板(在msdn發現)爲:

<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> 

正如你可以看到你剝出所有觸發器,更不用說視覺樣式。

我會拿這個代碼,把你的多邊形代替ContentPresenter

+0

這些頁面給出的例子並不是原始模板,請參見[這個問題](http://stackoverflow.com/questions/1559261/control-template-for-existing-controls-in-wpf)。 – 2012-03-27 10:04:44

+0

@ H.B。謝謝,我糾正了。 – 2012-03-27 10:11:04

相關問題