覆盖控件形状的Application.Resources样式

时间:2017-01-12 10:53:33

标签: wpf xaml

我试图为两个具有Application.Resource样式的按钮实现一个形状,如下所示:

  <Style TargetType="Button">

        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="BorderBrush" Value="#00FFFFFF"/>
        <Setter Property="Foreground" Value="WhiteSmoke"/>
        <Setter Property="FontFamily" Value="Miryad Pro"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="Button">
                    <Border x:Name="ButtonBorder" 
              CornerRadius="3" 
              BorderThickness="2" 
              Background="#33FFFFFF"  
              BorderBrush="#00FFFFFF"
              RenderTransformOrigin="0.5,0.5">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="1.7*"/>
                            </Grid.RowDefinitions>
                            <ContentPresenter x:Name="ButtonContentPresenter"
                            VerticalAlignment="Center"  
                            HorizontalAlignment="Center" 
                            Grid.RowSpan="2" />
                        </Grid>

                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="RenderTransform" TargetName="ButtonBorder">
                                <Setter.Value>
                                    <TransformGroup>
                                        <ScaleTransform ScaleX="0.9" ScaleY="0.9"/>
                                    </TransformGroup>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>


                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我想重新塑造的按钮有这个代码:

 <Button x:Name="kontakInterface_btn" VerticalAlignment="Stretch" HorizontalAlignment="Right" Width="40" Height="90" Margin="0,20,0,200"     Click="kontakInterface_btn_Click">

            <Button.Background>
                <VisualBrush>
                    <VisualBrush.Visual>
                        <Canvas>
                            <Path Opacity="0.3" Fill="#ffffffff" Data="F1 M 8.126,171.916 L 58.216,160.074 C 62.411,158.889 65.125,149.942 65.125,147.337 L 65.125,32.680 C 65.125,30.074 61.053,20.469 58.216,19.943 L 8.126,8.496"/>
                        </Canvas>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Button.Background>




            <Image Source="C:\Users\PID Projekt I5\Documents\Visual Studio 2015\Projects\ArduinoSMS_sender\ArduinoSMS_sender\Ikone\directory-icon.png"/>

        </Button>

为了使重新整形代码有效,我需要覆盖或更改/添加什么?对我来说,XAML真的很痛苦。

2 个答案:

答案 0 :(得分:0)

我在按钮上添加了一个属性,让它神奇地起作用:

Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"

我用它来删除按钮的背景。

答案 1 :(得分:0)

我可能误解了你的问题,但如果你只是想把这个风格应用到你的按钮上,你可以给它一个x:Key:

<Button x:Name="kontakInterface_btn" Style="{StaticResource myButtonStyle}">

...并使用StaticResource标记扩展将其应用于任何按钮:

import scala.io.Source
val inclusions = Source.fromFile("filename.txt").getLines.toSet

yourRDD.filter(t => inclusions.contains(t._1))

如果您没有指定x:Key,则Style应自动应用于与Style相同范围的所有Button元素。有关样式和模板在WPF中的工作原理的信息,请参阅MSDN:https://msdn.microsoft.com/en-us/library/ms745683(v=vs.110).aspx