简体   繁体   English

在wpf中为Button / ListBox添加/设置Border属性

[英]Add / set Border property for Button/ListBox in wpf

The Button and ListBox code are already using one static resource: Button和ListBox代码已经在使用一种静态资源:

<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>

And I only need to add this: 我只需要添加以下内容:

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

How to do it ? 怎么做 ?

I have tryed : 我尝试过:

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

for list box but still no good( 对于列表框,但还是不好(

you need to use window/userControl.resources (depends what your using), the following example uses UserControl, to use it for window just change the usercontrol to window: 您需要使用window / userControl.resources(取决于您的使用方式),以下示例使用UserControl,要将其用于window,只需将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>

in your border: 在您的边界:

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

If you want to set this as a global style, just embedd it into the xaml of your Application class like this: 如果要将此设置为全局样式,只需将其嵌入到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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM