【问题标题】:Overriding Application.Resources style for control shape覆盖控件形状的 Application.Resources 样式
【发布时间】:2017-01-12 21:17:45
【问题描述】:

我试图为两个具有 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 对我来说真的很痛苦。

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    我为按钮添加了一个属性,让它神奇地工作:

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

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

    【讨论】:

      【解决方案2】:

      我可能误解了您的问题,但如果您只是想将样式应用于您的 Button,您可以给它一个 x:Key:

      <Style x:Key="myButtonStyle" ... >
      

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多