在wpf中为按钮/列表框添加/设置边框属性

本文关键字:添加 设置 边框 属性 列表 按钮 wpf | 更新日期: 2023-09-27 18:11:36

按钮和列表框代码已经使用了一个静态资源:

<StackPanel Orientation = "Vertical">
     <Button x:Name="ololo1"  Margin="0,10,0,0" Command="{Binding CommandDump}" Background="Purple" Foreground="White" FontFamily="{StaticResource FontFamily-Sketch}">Text</Button>
...
    <ListBox local:ListBoxBehaviors.AutoSizeItemCount="3" ItemsSource="{Binding ItemsCollection}" HorizontalContentAlignment="Center" DisplayMemberPath="Item1" ScrollViewer.VerticalScrollBarVisibility="Disabled"/>
</StackPanel>

我只需要加上这个:

 <Style TargetType="Border">
            <Setter Property="CornerRadius" Value="10"/>
        </Style>

怎么做?

I have try:

 <ListBox ...>
        <ListBox.Resources>
            <Style TargetType="Border">
                <Setter Property="CornerRadius" Value="10"/>
            </Style>
        </ListBox.Resources>
    <!--...-->
</ListBox>
(

在wpf中为按钮/列表框添加/设置边框属性

)

如果你想将其设置为全局样式,只需将其嵌入到Application类的xaml中,如下所示:

<Application x:Class="YourNamespace"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
            <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
            <Style TargetType="{x:Type Border}">
                <Setter Property="CornerRadius" Value="10"/>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

你需要使用window/userControl。资源(取决于你使用什么),下面的例子使用UserControl,将它用于窗口只需将UserControl更改为window:

<UserControl.Resources>
<Style TargetType="{x:Type Button}" x:Key="BorderStyle">     
<Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="3,0,0,0" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
 </Style>
</UserControl.Resources>

:

<Button Style="{StaticResource BorderStyle}"/>