【问题标题】:How can I use a button style as a template for the other buttons?如何使用按钮样式作为其他按钮的模板?
【发布时间】:2021-03-18 12:44:28
【问题描述】:

我有一个具有以下属性的按钮:

<Grid Margin="0,0,593,0">
    <Button x:Name="btnToolbarClose" Content="X" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" VerticalContentAlignment="Center"  HorizontalContentAlignment="Center" FontSize="12" FontFamily="Arial" Height="23" Width="30" Margin="0,-1,-1,0"/>
    <Button x:Name="btnToolbarMax"  Content="£" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Grid.Column="1" HorizontalAlignment="Right" Margin="0,-1,29,0" VerticalAlignment="Top" FontFamily="Wingdings 2" VerticalContentAlignment="Center" FontSize="12" Height="23" Width="30" />
    <Button x:Name="btnToolbarMin" Content="─" Grid.Column="1" HorizontalAlignment="Right" Margin="0,-1,59,0" VerticalAlignment="Top" VerticalContentAlignment="Bottom" HorizontalContentAlignment="Center" FontSize="14" Height="23" Width="30" >
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Background" Value="{x:Null}"/>
                <Setter Property="BorderBrush" Value="{x:Null}"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="0">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Red"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

</Grid>

现在我希望我不必为每个按钮插入此按钮样式,而只需将此样式用于具有绑定的其他按钮?因此我只需编写一次此样式并将其用于另一个按钮?

【问题讨论】:

    标签: wpf xaml data-binding


    【解决方案1】:

    重用 WPF 控件样式的一种简单方法是使用 ResourceDictionariesLink to MSDN

    ResourceDictionary 添加到您的项目后,您可以通过将其添加到XAML 的开头来引用它,如下所示:

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="./YourNewDictionary.xaml"/> // This points to the location of your dictionary.
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    

    您可以在新添加的ResourceDictionary 中定义您的自定义样式,有点像这样:

    <Style x:Key="YourButtonStyle" TargetType="Button">
        <Setter Property="Margin" Value="5"/>
        And all other customized properties....
    </Style>
    

    然后您可以像这样在控件中引用您的自定义样式:

    <Button x:Name="YourButton" Style="{StaticResource YourButtonStyle}">
    

    这让您可以轻松创建自定义样式,您可以在整个项目中重复使用这些样式,而不会在您的 XAML 文件中造成太多混乱。

    【讨论】:

    • 另请注意,如果您想自动将样式应用于资源的范围内的所有按钮(无需每次都使用StaticResource 或@987654330 引用它@),您可以删除 x:Key="YourButtonStyle" 以使其隐式
    • 同时避免将字典添加到窗口资源中,因为每次实例化窗口时都会创建这些字典,样式的适当位置应位于 App.xaml 中。样式被隐式应用,除非那些在 DataTemplates 中,那么你需要使用 x:Key。
    猜你喜欢
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 2014-11-05
    • 2019-08-01
    • 2017-04-04
    • 1970-01-01
    相关资源
    最近更新 更多