zoukankan      html  css  js  c++  java
  • 【Win 10应用开发】AdaptiveTrigger在自定义控件中是可以触发的

    前些天,看到有网友给我留言,说AdaptiveTrigger在自定义控件(模板化控件)中不能触发。因为当时我正在写其他的代码,就没有去做实验来验证,于是我就给这位网友提了使用GotoVisualState的方法来自己切换状态。

    今天有空,我就做了做测试,发现AdaptiveTrigger触发器在模板化控件的控件件模板中是可以触发的。

    首先,向应用程序项目添加一个新的模板化控件,我叫它为MyControl。如下图。

    然后打开Generic.xaml文件,找到新控件的样式,将模板修改为:

                    <ControlTemplate TargetType="local:MyControl">
                        <Border
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            x:Name="bd">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup>
                                    <VisualState>
                                        <VisualState.StateTriggers>
                                            <AdaptiveTrigger MinWindowWidth="700" />
                                        </VisualState.StateTriggers>
                                        <VisualState.Setters>
                                            <Setter Target="bd.Background" Value="Red" />
                                        </VisualState.Setters>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </Border>
                    </ControlTemplate>

    上面模板中应用了可视化状态,并且状态用AdaptiveTrigger来触发,当应用程序窗口的宽度>= 700时就会触发,把模板中Border的背景颜色改为红色。

    然后打开MainPage,在页面上声明一个MyControl实例。

        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <local:MyControl />
        </Grid>

    最后,运行应用程序,效果如下。

    随后,把窗口的宽度拉大,会看到背景变成了红色。

    通过这个试验,我发现:AdaptiveTrigger触发器在自定义控件的模板中是可以使用的

    示例源代码下载

  • 相关阅读:
    有没有对象???new一个???
    原生数组的方法--翻转
    rclone的基本用法
    golang 文件操作
    记一次挖矿程序处理 firstpress
    python 第三方库 网络 requests
    python 第三方库 时间 arrow
    ansible playbook loop 翻译
    硬盘性能测试工具之bonnie++
    磁盘性能测试工具之fio
  • 原文地址:https://www.cnblogs.com/tcjiaan/p/4815840.html
Copyright © 2011-2022 走看看