c# - 自适应触发器和数据模板

标签 c# windows-runtime windows-10 win-universal-app uwp

AdaptiveTrigger 能否在 DataTemplate 中工作?

这是我用来自定义 ShellNavigation 的代码,它工作正常,但视觉状态除外。他们不会触发任何东西。

<shell:ShellHeadView x:Key="ShellHeadView_01">
    <shell:ShellHeadView.ContentTemplate>
        <DataTemplate>
            <Grid Margin="20,0">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup>
                        <VisualState x:Name="GreenBackgroundVisualState">
                            <VisualState.Setters>
                                <Setter Target="headViewLeft.Background" Value="Green" />
                            </VisualState.Setters>
                            <VisualState.StateTriggers>
                                <AdaptiveTrigger MinWindowWidth="1000"/>
                            </VisualState.StateTriggers>
                        </VisualState>
                        <VisualState x:Name="OrangeBackgroundVisualState">
                            <VisualState.Setters>
                                <Setter Target="headViewLeft.Background" Value="Orange" />
                            </VisualState.Setters>
                            <VisualState.StateTriggers>
                                <AdaptiveTrigger MinWindowWidth="2000"/>
                            </VisualState.StateTriggers>
                        </VisualState>
                        <VisualState x:Name="RedBackgroundVisualState">
                            <VisualState.Setters>
                                <Setter Target="headViewLeft.Background" Value="Red" />
                            </VisualState.Setters>
                            <VisualState.StateTriggers>
                                <AdaptiveTrigger MinWindowWidth="3000"/>
                            </VisualState.StateTriggers>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="0" x:Name="headViewLeft" Width="100" Height="90">

                </Grid>

最佳答案

尝试像这样将 DataTemplate 包装在 UserControl 中 -

<DataTemplate>
    <UserControl>
        <Grid>
            <VisualStateManager.VisualStateGroups>
            ...
        </Grid>
    </UserControl>
</DataTemplate>

看起来任何具有 Content 属性的 Control 都可以工作。这就是 UserControl 起作用的原因,ContentControl 也是如此。

因此,如果您将 UserControl 替换为 ContentControl 并为其指定一个空的 Style。它也应该有效。

<Style x:Key="EmptyContentControlStyle" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl" />
        </Setter.Value>
    </Setter>
</Style>

<DataTemplate>
    <ContentControl Style="{StaticResource EmptyContentControlStyle}">
        <Grid>
            <VisualStateManager.VisualStateGroups>
            ...
        </Grid>
    </ContentControl>
</DataTemplate>

关于c# - 自适应触发器和数据模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32088500/

相关文章:

c# - 在哪里可以找到 MSDN 库图标的含义?

c# - 如何防止自动实现的属性被序列化?

c# - WinRT 创建一个文件(如果该文件不存在)并知道该文件已创建

javascript - WinJS.UI.ListView - 使用 JavaScript 构建模板时刷新项目

windows-10 - 如何在 Windows 10 上获得 Yo 识别的箭头键?

c# - 将 base64 字符串写入不带回车符的文件

c# - 如何禁用 Identity 2.0 中的用户?

c# - 从 httpwebrequest 切换到 httpclient,我不知道如何发送我的 header ?

sql - 使用 SSMS 2016 : Object cannot be cast from DBNull to other types 时出错

c# - 以编程方式关闭 Windows Phone 应用程序