样式DataTrigger不评估初始加载

时间:2016-03-16 21:23:35

标签: c# wpf

标题说明了一切

<Page.Resources>
    <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>

    <DataTemplate x:Key="level1">
        <StackPanel Orientation="Vertical">
            <TextBlock x:Name="GroupTitle" FontSize="16" FontWeight="Bold" Foreground="#FF000532" Text="{Binding name}"/>
            <TextBlock x:Name="GroupDescription" Text="{Binding description}"/>
            <TextBlock x:Name="Depth" Text="{Binding childDepth}"/>
            <ItemsControl ItemsSource="{Binding settings}">

            </ItemsControl>
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="level2">
        <StackPanel Orientation="Vertical">
            <TextBlock x:Name="GroupTitle" FontSize="16" FontWeight="Bold" Foreground="#FF000532" Text="{Binding name}"/>
            <TextBlock x:Name="GroupDescription" Text="{Binding description}"/>
            <TextBlock x:Name="Depth" Text="{Binding childDepth}"/>
            <ItemsControl ItemsSource="{Binding settings}">

            </ItemsControl>
            <TabControl ItemsSource="{Binding groups}">
                <TabControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding name}" />
                    </DataTemplate>
                </TabControl.ItemTemplate>
                <TabControl.ContentTemplate>
                    <DataTemplate>
                        <ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource level1}"/>
                    </DataTemplate>
                </TabControl.ContentTemplate>
            </TabControl>
        </StackPanel>
    </DataTemplate>
</Page.Resources>
<Grid>
    <ContentControl x:Name="SettingsDisplayer" Content="{Binding settingGroup, ElementName=page}">
        <ContentControl.Resources>
            <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
        </ContentControl.Resources>
        <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                <Setter Property="ContentTemplate" Value="{StaticResource level1}" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=childDepth}" Value="1">
                        <Setter Property="ContentTemplate" Value="{StaticResource level1}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=childDepth}" Value="2">
                        <Setter Property="ContentTemplate" Value="{StaticResource level2}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=childDepth}" Value="3">
                        <Setter Property="ContentTemplate" Value="{StaticResource level3}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=childDepth}" Value="4">
                        <Setter Property="ContentTemplate" Value="{StaticResource level4}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>
</Grid>

childDepth的getter甚至没有被访问,因为我在那里放了一个控制台跟踪来知道它是什么时候。该属性已填充,因为它在TextBlock s的深度DataTemplate中显示时显示正确的数量。

我班上的相关财产:

    public int childDepth { get { Console.WriteLine(this.getDepth()); return this.getDepth(); } }

无论是什么,孩子的深度是什么,它总是跳过触发器,只使用level1模板。

1 个答案:

答案 0 :(得分:0)

我觉得很傻,所以显然绑定ContentControl的内容并不等同于将数据绑定到ContentControl,因此我需要更具体地将我的DataTrigger绑定作为我的原始目标DP,因为它不会只是绑定父容器。

这有效:

<DataTrigger Binding="{Binding settingGroup.childDepth, ElementName=page}" Value="1">
    <Setter Property="ContentTemplate" Value="{StaticResource level1}" />
</DataTrigger>