最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WPF?Trigger改變屬性無(wú)效問(wèn)題排查示例詳解

 更新時(shí)間:2023年10月07日 14:38:00   作者:光法V3  
這篇文章主要為大家介紹了WPF?Trigger改變屬性無(wú)效問(wèn)題排查示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

WPF Trigger改變屬性無(wú)效問(wèn)題排查

  • WPF 使用trigger改變這個(gè)按鈕的顏色,結(jié)果發(fā)現(xiàn)實(shí)際沒(méi)有生效。第一眼感覺(jué)很奇怪,后來(lái)一想確實(shí)是這樣,因?yàn)榻M件設(shè)置的優(yōu)先級(jí)問(wèn)題,直接在控件設(shè)置比模板里的設(shè)置屬性的優(yōu)先級(jí)高,覆蓋了模板的設(shè)置。
<Button Button.Name="PART_DropDownButton" Foreground="Blue">
            <Control.Template>
                <ControlTemplate ControlTemplate.TargetType="{x:Type Button}">
                    <Border FrameworkElement.Cursor="Hand">
                        <Grid>
                            <Path
                                x:Name="filterIcon"
                                Width="16"
                                Height="16"
                                Margin="0,0,0,0"
                                Fill="{TemplateBinding Foreground}"
                                Path.Data="M3.42591 3H12.5741C12.6566 3 12.7373 3.02543 12.8065 3.07319C12.8756 3.12095 12.9302 3.189 12.9636 3.26905C12.997 3.3491 13.0077 3.43772 12.9945 3.52413C12.9813 3.61054 12.9447 3.69102 12.8892 3.75579L9.38851 7.84104C9.31723 7.92421 9.27774 8.03258 9.27774 8.14498V11.3432C9.27774 11.4176 9.26043 11.4909 9.22735 11.5564C9.19426 11.622 9.14642 11.6779 9.08808 11.7192L7.38443 12.9241C7.32028 12.9695 7.24574 12.9955 7.16874 12.9995C7.09174 13.0034 7.01517 12.9851 6.9472 12.9465C6.87923 12.9079 6.82241 12.8505 6.78279 12.7803C6.74318 12.7102 6.72226 12.6299 6.72226 12.5482V8.14498C6.72226 8.03258 6.68277 7.92421 6.61149 7.84104L3.11076 3.75579C3.05526 3.69102 3.01869 3.61054 3.00549 3.52413C2.99229 3.43772 3.00303 3.3491 3.03641 3.26905C3.06979 3.189 3.12437 3.12095 3.19352 3.07319C3.26267 3.02543 3.3434 3 3.42591 3Z"
                                Stretch="None" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="Red" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Control.Template>
        </Button>

那么怎么修改呢

最簡(jiǎn)單的就是增加style

在style里設(shè)置這個(gè)默認(rèn)的字色

<Button Button.Name="PART_DropDownButton">
            <Button.Style>
                <Style TargetType="Button">
                    <Setter Property="Foreground" Value="Blue" />
                </Style>
            </Button.Style>
            <Control.Template>
               ... 代碼省略
            </Control.Template>
</Button>

搞定。

直接修改模板里的顏色值為固定值

其實(shí)還有很多修改方式,比如說(shuō),直接修改模板里的顏色值為固定值,不使用TemplateBinding,在這里也能解決問(wèn)題,但是實(shí)際開(kāi)發(fā)中擴(kuò)展性會(huì)有點(diǎn)問(wèn)題;另外一種就是使用附加屬性,附加屬性,附加屬性不定義在宿主內(nèi)部,一般定義在公共的類(lèi)中,提供給全局使用,所以,如果你想定制一個(gè)樣式,設(shè)置某兩個(gè)屬性,但是控件不支持時(shí),可以使用附加屬性在style的模板中使用改屬性,這樣可以兼容統(tǒng)一樣式和個(gè)別自定義樣式的修改需求。只是解決這個(gè)問(wèn)題是顯得太復(fù)雜了,不過(guò)可以了解下用途,下面來(lái)簡(jiǎn)單介紹下,

// 從這里可以看出你可以定制各種各樣的屬性,來(lái)改變控件的樣式,圓角,顏色等等
    public class ControlAttachProperty
    {
        #region 圓角
        public static CornerRadius GetCornerRadius(DependencyObject obj)
        {
            return (CornerRadius)obj.GetValue(CornerRadiusProperty);
        }
        public static void SetCornerRadius(DependencyObject obj, CornerRadius value)
        {
            obj.SetValue(CornerRadiusProperty, value);
        }
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.RegisterAttached("CornerRadius", typeof(CornerRadius), typeof(ControlAttachProperty), new PropertyMetadata(null));
        #endregion
        #region 附加顏色1
        public static SolidColorBrush GetAttachColor(DependencyObject obj)
        {
            return (SolidColorBrush)obj.GetValue(AttachColorProperty);
        }
        public static void SetAttachColor(DependencyObject obj, SolidColorBrush value)
        {
            obj.SetValue(AttachColorProperty, value);
        }
        public static readonly DependencyProperty AttachColorProperty =
            DependencyProperty.RegisterAttached("AttachColor", typeof(SolidColorBrush), typeof(ControlAttachProperty), new PropertyMetadata(null));
        #endregion
        #region 附加顏色2
        public static SolidColorBrush GetAttachColor1(DependencyObject obj)
        {
            return (SolidColorBrush)obj.GetValue(AttachColor1Property);
        }
        public static void SetAttachColor1(DependencyObject obj, SolidColorBrush value)
        {
            obj.SetValue(AttachColor1Property, value);
        }
        public static readonly DependencyProperty AttachColor1Property =
            DependencyProperty.RegisterAttached("AttachColor1", typeof(SolidColorBrush), typeof(ControlAttachProperty), new PropertyMetadata(null));
        #endregion
        #region 附加圖片資源
        public static ImageSource GetAttachImageSource(DependencyObject obj)
        {
            return (ImageSource)obj.GetValue(AttachImageSourceProperty);
        }
        public static void SetAttachImageSource(DependencyObject obj, ImageSource value)
        {
            obj.SetValue(AttachImageSourceProperty, value);
        }
        public static readonly DependencyProperty AttachImageSourceProperty =
            DependencyProperty.RegisterAttached("AttachImageSource", typeof(ImageSource), typeof(ControlAttachProperty), new PropertyMetadata(null));
        #endregion
    }

在style中定義過(guò)濾按鈕的樣式,默認(rèn)的話黑色,當(dāng)鼠標(biāo)懸浮的時(shí)候是綠色。這是系統(tǒng)的統(tǒng)一樣式。

<Window.Resources>
        <Style x:Key="FilterButton" TargetType="{x:Type Button}">
            <Setter Property="local:ControlAttachProperty.AttachColor" Value="Black" />
            <Setter Property="local:ControlAttachProperty.AttachColor1" Value="Green" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate ControlTemplate.TargetType="{x:Type Button}">
                        <Border FrameworkElement.Cursor="Hand">
                            <Grid>
                                <Path
                                    x:Name="filterIcon"
                                    Width="16"
                                    Height="16"
                                    Margin="0,0,0,0"
                                    Fill="{TemplateBinding local:ControlAttachProperty.AttachColor}"
                                    Path.Data="M3.42591 3H12.5741C12.6566 3 12.7373 3.02543 12.8065 3.07319C12.8756 3.12095 12.9302 3.189 12.9636 3.26905C12.997 3.3491 13.0077 3.43772 12.9945 3.52413C12.9813 3.61054 12.9447 3.69102 12.8892 3.75579L9.38851 7.84104C9.31723 7.92421 9.27774 8.03258 9.27774 8.14498V11.3432C9.27774 11.4176 9.26043 11.4909 9.22735 11.5564C9.19426 11.622 9.14642 11.6779 9.08808 11.7192L7.38443 12.9241C7.32028 12.9695 7.24574 12.9955 7.16874 12.9995C7.09174 13.0034 7.01517 12.9851 6.9472 12.9465C6.87923 12.9079 6.82241 12.8505 6.78279 12.7803C6.74318 12.7102 6.72226 12.6299 6.72226 12.5482V8.14498C6.72226 8.03258 6.68277 7.92421 6.61149 7.84104L3.11076 3.75579C3.05526 3.69102 3.01869 3.61054 3.00549 3.52413C2.99229 3.43772 3.00303 3.3491 3.03641 3.26905C3.06979 3.189 3.12437 3.12095 3.19352 3.07319C3.26267 3.02543 3.3434 3 3.42591 3Z"
                                    Stretch="None" />
                                <Path
                                    x:Name="filterIcon_mouseover"
                                    Width="16"
                                    Height="16"
                                    Margin="0,0,0,0"
                                    Fill="{TemplateBinding local:ControlAttachProperty.AttachColor1}"
                                    Path.Data="M3.42591 3H12.5741C12.6566 3 12.7373 3.02543 12.8065 3.07319C12.8756 3.12095 12.9302 3.189 12.9636 3.26905C12.997 3.3491 13.0077 3.43772 12.9945 3.52413C12.9813 3.61054 12.9447 3.69102 12.8892 3.75579L9.38851 7.84104C9.31723 7.92421 9.27774 8.03258 9.27774 8.14498V11.3432C9.27774 11.4176 9.26043 11.4909 9.22735 11.5564C9.19426 11.622 9.14642 11.6779 9.08808 11.7192L7.38443 12.9241C7.32028 12.9695 7.24574 12.9955 7.16874 12.9995C7.09174 13.0034 7.01517 12.9851 6.9472 12.9465C6.87923 12.9079 6.82241 12.8505 6.78279 12.7803C6.74318 12.7102 6.72226 12.6299 6.72226 12.5482V8.14498C6.72226 8.03258 6.68277 7.92421 6.61149 7.84104L3.11076 3.75579C3.05526 3.69102 3.01869 3.61054 3.00549 3.52413C2.99229 3.43772 3.00303 3.3491 3.03641 3.26905C3.06979 3.189 3.12437 3.12095 3.19352 3.07319C3.26267 3.02543 3.3434 3 3.42591 3Z"
                                    Stretch="None"
                                    Visibility="Collapsed" />
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="filterIcon_mouseover" Property="Visibility" Value="Visible" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
// 按鈕使用
    <Grid>
        <Button Button.Name="PART_DropDownButton" Style="{StaticResource FilterButton}" />
    </Grid>

那么如果有個(gè)需求:有一個(gè)要默認(rèn)藍(lán)色,懸浮紅色呢,只要

使用按鈕的時(shí)候使用如下代碼即可,這樣就可以實(shí)現(xiàn)了樣式的統(tǒng)一和個(gè)性簡(jiǎn)單的定制。

<Button
            local:ControlAttachProperty.AttachColor="Blue"
            local:ControlAttachProperty.AttachColor1="Red"
            Button.Name="PART_DropDownButton"
            Style="{StaticResource FilterButton}" />

以上就是WPF Trigger改變屬性無(wú)效問(wèn)題排查示例詳解的詳細(xì)內(nèi)容,更多關(guān)于WPF Trigger屬性無(wú)效排查的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#實(shí)現(xiàn)快速查詢文件的方法

    C#實(shí)現(xiàn)快速查詢文件的方法

    這篇文章介紹了C#實(shí)現(xiàn)快速查詢文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#實(shí)現(xiàn)關(guān)機(jī)功能

    C#實(shí)現(xiàn)關(guān)機(jī)功能

    這篇文章介紹了C#實(shí)現(xiàn)關(guān)機(jī)功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#實(shí)現(xiàn)在控制臺(tái)輸入密碼顯示星號(hào)的方法

    C#實(shí)現(xiàn)在控制臺(tái)輸入密碼顯示星號(hào)的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)在控制臺(tái)輸入密碼顯示星號(hào)的方法,感興趣的小伙伴們可以參考一下
    2016-04-04
  • C#實(shí)現(xiàn)Word轉(zhuǎn)PDF的方法總結(jié)

    C#實(shí)現(xiàn)Word轉(zhuǎn)PDF的方法總結(jié)

    這篇文章主要為大家詳細(xì)介紹了C#中實(shí)現(xiàn)Word轉(zhuǎn)PDF的常用方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,有需要的小伙伴可以參考下
    2023-10-10
  • C# 實(shí)現(xiàn)superpowers進(jìn)化功能示例

    C# 實(shí)現(xiàn)superpowers進(jìn)化功能示例

    本文提供了一個(gè)“超級(jí)能力進(jìn)化”功能示例,包括基礎(chǔ)類(lèi)定義、進(jìn)化邏輯和測(cè)試用例,建議增加MaxLevel限制、特殊進(jìn)化條件、能力樹(shù)系統(tǒng)、屬性成長(zhǎng)系統(tǒng),并完善異常處理和經(jīng)驗(yàn)值驗(yàn)證邏輯,以增強(qiáng)功能的靈活性和復(fù)雜性
    2026-04-04
  • C#無(wú)損高質(zhì)量壓縮圖片實(shí)現(xiàn)代碼

    C#無(wú)損高質(zhì)量壓縮圖片實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了C#無(wú)損高質(zhì)量壓縮圖片的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 如何給C#變量取名字

    如何給C#變量取名字

    本文主要介紹了如何給C#變量取名字,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Unity實(shí)現(xiàn)Flappy Bird游戲開(kāi)發(fā)實(shí)戰(zhàn)

    Unity實(shí)現(xiàn)Flappy Bird游戲開(kāi)發(fā)實(shí)戰(zhàn)

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)Flappy Bird游戲開(kāi)發(fā)實(shí)戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Linq兩個(gè)List集合取交集的實(shí)現(xiàn)

    Linq兩個(gè)List集合取交集的實(shí)現(xiàn)

    這篇文章主要介紹了Linq兩個(gè)List集合取交集的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • C#迷你猜數(shù)實(shí)例分析

    C#迷你猜數(shù)實(shí)例分析

    這篇文章主要介紹了C#迷你猜數(shù),實(shí)例分析C#操作數(shù)字及數(shù)組的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03

最新評(píng)論

民权县| 弥勒县| 铜川市| 石首市| 芜湖县| 乳山市| 麻城市| 南靖县| 昌吉市| 桦川县| 韶山市| 峨山| 木里| 洛扎县| 全椒县| 伊金霍洛旗| 比如县| 新化县| 塔河县| 余江县| 昌吉市| 乐亭县| 青阳县| 龙口市| 阳信县| 浪卡子县| 恩平市| 广东省| 永兴县| 岐山县| 赤壁市| 揭阳市| 明光市| 鞍山市| 金华市| 扬中市| 佛教| 苗栗市| 景洪市| 运城市| 桦甸市|