WPF Button 樣式資源設置&后台生成button樣式


起這個名字,如果你被題目吸引了內容卻不是你想要的,sorry啊.

網上很多講WPF的樣式設置,基本上是在XAML中生成,我遇到的是,自己后台生成button,設定了背景圖片,想做到鼠標划過button時(獲得焦點同理)有發光的效果,下面記錄的內容:

先在項目xaml的resources中加入comtrolTemplate的內容,下面這里包括了鼠標划過及獲得焦點事件.

<Window.Resources>
        <ControlTemplate TargetType="Button" x:Key="ButtonTemplate">
            <Border Name="border" CornerRadius="8"  Background="{TemplateBinding Background}">
                <ContentPresenter Name="content" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                              RecognizesAccessKey="True" 
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect BlurRadius="10" Color="Black" Direction="0" Opacity="0.6"
 RenderingBias="Performance" ShadowDepth="0"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect BlurRadius="10" Color="Black" Direction="0" Opacity="0.6"
 RenderingBias="Performance" ShadowDepth="0"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Window.Resources>

第二步,在cs中生成的button,這里的"ButtonTemplate"跟第一步的x:Key="ButtonTemplate"同名

 Button tbi = new Button()
                    {
                        Width =200,
                        Height=100,
                        Template = this.Resources["ButtonTemplate"] as ControlTemplate
                    };

 

 

WPF獲得焦點后如何讓邊框發光http://blog.kiccp.com/200.html

原來學習來源WPF中的ControlTemplate(控件模板):http://www.cnblogs.com/zhouyinhui/archive/2007/03/28/690993.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM