WPF樣式和資源


<Window.Resources>
  <FontFamily x:key="ButtonFontFamily">Time New Roman</FontFamily>
  <sys:Double x:key="ButtonFontSize">18</s:Double>
  <FontWeight x:key="ButtonFontWeight">Bold</FontWeight>
</Window.Resources>

1:資源里定義了三個資源,分別設置對象屬性
接下來應用資源:

<Button FontFamily="{StaticResource ButtonFontFamily}"
FontWeight=="{StaticResource ButtonFontWeight}"
FontSize=="{StaticResource ButtonFontSize}"
</Button>


2:另一種方式:(WPF中的一個元素都可以使用一個樣式,只能使用一個樣式

創建一個獨立的資源:

<Window.Resources>
  <Style x:key="BigFontButtonStyle">
    <setter Property="control.FontFamily" Value="Tinmes New     Roman"/>
    <Setter Property="control.FontSize" Value="18"/>
    <Setter Property="control.FontWeight" Value="Bold"/>
  </Style>
</Window.Resources>

應用這個獨立的樣式資源:

<Button Sytle="{StaticResource BigFontButtonStyle}">a Customized</Button>

Style類的屬性:
Setters:設置屬性值並自動關聯事件處理程序的Sette對象或EventSetter對象的集合

Triggers:能夠自動改變樣式設置的對象集合

Resources

BasedOn:通過該屬性可以創建繼承其他樣式設置的更復雜樣式

TargetType:該屬性表示應用樣式的元素的類型

3:關聯的事件處理程序

<Style x:key="MouseOverHighLightStyle">
  <EventSetter Event="TextBlock.MouseEnter" Handler="element_MouseEnter"/>
  <EventSetter Event=="TextBlock.MouseLeave" Handler="element_MouseLeave"/>
  <Setter Property=TextBlock.Padding" Value="5"/>
</Style>

private void element_MouseEnter(object sender,MouseEventArgs e)
{

  ((TextBlock)Senter.Background=new SolidColorBrush(Colors.LightGoldenrodYellow);

}

事件element_MouseLeave同樣的道理

接下來應用給樣式;

<Textklock Style="{StaticResouce MouseOverHightStyle}"> Hover Over me</Textklock >

4:簡單觸發器

<Style x:key="BigFontButton">
  <Style.Setters>
    <Setter Property="Control.FontFamily" Value="Time NEw Roman"/>
    <Setter Property="Control.FontSize" Value="18"/>
  </Style.Setters>
  
  <Style.Triggers>
    <Triggers Property="Control.IsFocused" Value="True">
    <Setter Property="Control.Foreground" Value="darkRed"/>
  </Style.Triggers>
</Style>
也可以定義多個觸發器

 5:比較復雜的,這里用到了”行為“:

View Code
 <Style x:Key="OuterGlowStyle" TargetType="{x:Type FrameworkElement}">
            <Style.Triggers>
                <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>
                <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>
            </Style.Triggers>
        </Style>

6:RelativeSource相對路徑

    StaticSource絕對路徑

    ElementSource通常綁定某個控件的Value


免責聲明!

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



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