style的繼承

第一種方式:瞄準(zhǔn)控件的基類

如下例所示,繼承ContentControl的控件,都可以使用這個(gè)Style

<Window.Resources>
    <Style x:Key="ContentControlStyle" TargetType="ContentControl">
        <Setter Property="Background" Value="Pink"></Setter>
        <Setter Property="FontSize" Value="12"></Setter>
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Label Style="{StaticResource ContentControlStyle}" Content="按鈕和文本框使用了基于ContentControl的Style"></Label>
    <Button Style="{StaticResource ContentControlStyle}" Content="按鈕" Grid.Row="1"></Button>
</Grid>

第二種方式:繼承

<Window.Resources>
    <Style x:Key="baseStyle" TargetType="Button">
        <Setter Property="Background" Value="Pink"></Setter>
        <Setter Property="FontSize" Value="12"></Setter>
    </Style>
    <Style x:Key="childStyle" TargetType="Button" BasedOn="{StaticResource baseStyle}">
        <Setter Property="FontSize" Value="50"></Setter>
    </Style>
</Window.Resources>
<Grid>
    <Button Style="{StaticResource childStyle}" Content="按鈕"></Button>
</Grid>

就近原則:父類子類同時(shí)設(shè)定屬性,調(diào)用子類,以子類為準(zhǔn)

示例代碼

https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/Style 的StyleInheritDemo和StyleInheritDemo2

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,917評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,172評(píng)論 4 61
  • 在一個(gè)深秋的早晨,他坐在公司的九樓辦公室,喝著秘書送來的拿鐵咖啡,過了五分鐘的時(shí)候,辦公室的門敲響了,趙總,你今天...
    efe3691c071a閱讀 1,008評(píng)論 0 0
  • 和亮靚.27d5《贈(zèng)詩張琦峰》 今天早上看到簡(jiǎn)友亮靚27d5的《贈(zèng)詩張琦峰》,因有同感,故和之。 張張?jiān)娖?..
    林木成蔭閱讀 568評(píng)論 10 32
  • 三行詩 | 老爸 你不善言談 也沒多少文化 但無論見誰都笑哈...
    大貴貴丿閱讀 580評(píng)論 2 7

友情鏈接更多精彩內(nèi)容