如何使用布尔值作为触发器

时间:2016-03-29 14:46:51

标签: c# visual-studio xaml triggers win-universal-app

我需要一个VisualStateTrigger,它对ListView.SelectedIndex>做出反应。 -1

我的XAML看起来像这样:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
      EntranceNavigationTransitionInfo.IsTargetElement="True">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <VisualState x:Name="wideView">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="960"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="ListColumn.Width" Value="420"/>
                    <Setter Target="DetailColumn.Width" Value="*"/>
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="narrowDetailView">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0"/>

                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="ListColumn.Width" Value="0"/>
                    <Setter Target="DetailColumn.Width" Value="*"/>
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="narrowListView">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="ListColumn.Width" Value="*"/>
                    <Setter Target="DetailColumn.Width" Value="0"/>
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="ListColumn" Width="0"/>
        <ColumnDefinition x:Name="DetailColumn" Width="*"/>
    </Grid.ColumnDefinitions>
    <ListView x:Name="SelectionList"
              Grid.Column="0"/>
    <ListView x:Name="DetailsList"
              Grid.Column="1"/>
</Grid>

当SelectionList SelectedIndex == -1时,应选择narrowListView-Trigger,否则,narrowDetailsView-Trigger是所需的。

这只能在XAML中实现(如果是这样,最有效的方法是什么),还是需要在代码隐藏中解决这个问题。

1 个答案:

答案 0 :(得分:3)

WindowsStateTriggers图书馆是你的朋友。

<VisualState>
    <VisualState.StateTriggers>
        <triggers:CompareStateTrigger Value="{x:Bind someListView.SelectedIndex, Mode=OneWay}" CompareTo="-1" Comparison="GreaterThan" />
    </VisualState.StateTriggers>
<VisualState.Setters>