如何更改资源字典中按钮的角?

问题描述 投票:0回答:1

我需要帮助来创建我正在运行的代码的替代方案。非常感谢您的回答、支持和同行评审。

这个答案对我帮助很大。我的按钮形状确实符合我的预期。我现在的目标是,将所有必要的代码放入资源字典中。
现在,我必须为每个具有圆角的按钮添加一个

Button.Resources
。这就是我想要改变的。

这里是一些代码片段。该项目已在 GitHub 上公开。

这是View的一部分(完整代码如下)。

                <Button Content="Cancel" Command="{Binding CancelButtonCommand}">
                    <Button.Resources>
                        <Style TargetType="Border" BasedOn="{StaticResource buttonWithRoundCorners}" />
                    </Button.Resources>
                </Button>

这是我的

ResourceDictionary
,用于使我的应用程序中的所有按钮具有相同的样式。
此外,还配置了属性
CornerRadius

如何将
Border
的定义实现到
Button
的定义中。所以,我不需要将
Button.Resources
添加到我的
View
中的每个 Button

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Button">
        <Setter Property="Padding" Value="20 5 20 5"></Setter>
        <Setter Property="Margin" Value="5"></Setter>
    </Style>

    <Style TargetType="Border" x:Key="buttonWithRoundCorners">
        <Setter Property="CornerRadius" Value="4" />
    </Style>
    
</ResourceDictionary>

最后资源字典被合并。

<Application x:Class="PaperDeliveryWpf.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:PaperDeliveryWpf">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/TextBoxStyle.xaml" />
                <ResourceDictionary Source="Resources/PasswordBoxStyle.xaml" />
                <ResourceDictionary Source="Resources/ButtonStyle.xaml" />
                <ResourceDictionary Source="Resources/DataTemplate.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

在这里您可以找到我的

xaml
的完整
UserControl
,其中实现了
Button

<UserControl x:Class="PaperDeliveryWpf.UserControls.LoginUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:PaperDeliveryWpf.UserControls"
             xmlns:viewModels="clr-namespace:PaperDeliveryWpf.ViewModels" 
             xmlns:components="clr-namespace:PaperDeliveryWpf.UserControls.Components"
             mc:Ignorable="d" 
             d:DataContext="{d:DesignInstance Type=viewModels:LoginViewModel}"
             d:DesignHeight="450" d:DesignWidth="800">
    
    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="boolToVisibility" />
    </UserControl.Resources>

    <DockPanel>
        <!-- Header -->
        <Label DockPanel.Dock="Top" Content="Login Page" HorizontalAlignment="Center" FontSize="26" FontWeight="Bold" Margin="20" />

        <!-- Body -->
        <StackPanel DockPanel.Dock="Top">
            <Grid HorizontalAlignment="Center" FocusManager.FocusedElement="{Binding ElementName=txtUserName}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <!-- UserName components -->
                <Label Grid.Column="1" Grid.Row="0" Content="User Name" />
                <TextBox Grid.Column="2" Grid.Row="0" 
                         x:Name="txtUserName"
                         MinWidth="200"
                         Text="{Binding UiUserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}" />

                <!-- Password components; TODO - the DataErrors are not displayed properly -->
                <Label Grid.Column="1" Grid.Row="1" Content="Password" />
                <components:BindablePasswordBox Grid.Column="2" Grid.Row="1"
                                                x:Name="txtPassword"
                                                MinWidth="200"
                                                Password="{Binding UiPassword, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}" />

                <!-- Optional design; this is not hiding the entered values in the textbox -->
                <!--<TextBox Grid.Column="2" Grid.Row="1" 
                         x:Name="txtPassword"
                         MinWidth="200"
                         Text="{Binding UiPassword, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}" />-->
            </Grid>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <Button Content="Login" Command="{Binding LoginButtonCommand}">
                    <Button.Resources>
                        <Style TargetType="Border" BasedOn="{StaticResource buttonWithRoundCorners}" />
                    </Button.Resources>
                </Button>
                <Button Content="Cancel" Command="{Binding CancelButtonCommand}">
                    <Button.Resources>
                        <Style TargetType="Border" BasedOn="{StaticResource buttonWithRoundCorners}" />
                    </Button.Resources>
                </Button>
            </StackPanel>
        </StackPanel>
        
        <!-- Footer -->
    </DockPanel>

</UserControl>

wpf xaml button resourcedictionary
1个回答
0
投票

谢谢@emocht!

我重新尝试添加

Style.Resource
,这次成功了。不知何故,过去几个小时我使用了不正确的语法。

现在可以了,正如我所希望的那样:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Button">
        <Style.Resources>
            <Style TargetType="Border">
                <Setter Property="CornerRadius" Value="4" />
            </Style>
        </Style.Resources>
        <Setter Property="Padding" Value="20 5 20 5" />
        <Setter Property="Margin" Value="5" />
    </Style>

    <!-- not needed anymor; it is integrated into the button style, now -->        
    <Style TargetType="Border" x:Key="buttonWithRoundCorners">
        <Setter Property="CornerRadius" Value="4" />
    </Style>
    
</ResourceDictionary>
© www.soinside.com 2019 - 2024. All rights reserved.