ControlTemplate中的DataTrigger会破坏MultiBinding

时间:2016-11-07 11:57:54

标签: wpf xaml triggers controltemplate

我有跟随ControlTemplate的工作正常

<ControlTemplate x:Key="TotalCostsStatisticTemplate">
    <StackPanel x:Name="ContentHolderPanel" Visibility="Collapsed"
                Orientation="Horizontal" HorizontalAlignment="Center">
        <TextBlock x:Name="ValueTextBlock" VerticalAlignment="Center" 
                   Style="{DynamicResource PhasingValueTextStyle}">
            <TextBlock.Text>
                <MultiBinding Converter="{ttConverters:CustomDisplayFormatConverter}">
                    <Binding Path="FormatSettings" />
                    <Binding Path="AvailableStatistics.CostsFormat"/>
                    <Binding Path="TotalCosts" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </StackPanel>

    <ControlTemplate.Triggers>
        <DataTrigger Binding="{Binding ShowTotalCosts}" Value="True">
            <Setter TargetName="ContentHolderPanel" Property="Visibility" Value="Visible"/>
        </DataTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

但是如果我添加另一个DataTrigger,那么当第一次加载使用此模板的控件时,TextBlock.Text上的MultiBinding不起作用,即转换器CustomDisplayFormatConverter仅触发一次所有值为UnsetValue并且不会触发再次开火(再次重新加载窗口时工作正常)。

<ControlTemplate x:Key="TotalCostsStatisticTemplate">
    <StackPanel x:Name="ContentHolderPanel" Visibility="Collapsed"
                Orientation="Horizontal" HorizontalAlignment="Center">
        <TextBlock x:Name="ValueTextBlock" VerticalAlignment="Center" 
                   Style="{DynamicResource PhasingValueTextStyle}">
            <TextBlock.Text>
                <MultiBinding Converter="{ttConverters:CustomDisplayFormatConverter1}">
                    <Binding Path="FormatSettings" />
                    <Binding Path="AvailableStatistics.CostsFormat"/>
                    <Binding Path="TotalCosts" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </StackPanel>

    <ControlTemplate.Triggers>
        <DataTrigger Binding="{Binding ShowTotalCosts}" Value="True">
            <Setter TargetName="ContentHolderPanel" Property="Visibility" Value="Visible"/>
        </DataTrigger>

     <!--Trigger causing problem (breaking TextBlock.Text multi binding on first load) -->
        <DataTrigger Binding="{Binding IsCostsComplete}" Value="False">

            <Setter TargetName="ValueTextBlock" Property="Foreground" 
                    Value="{DynamicResource ManagerErrorBrush}"/>
        </DataTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

如果我将此DataTrigger放在TextBlock.Style中,它可以正常工作!

知道什么是错的吗?

更新:

以下是如何使用

<ControlTemplate x:Key="PhasingStatisticValuesTemplate">
    <StackPanel x:Name="ContentHolderPanel" Orientation="Horizontal" Visibility="{Binding IsValid, Converter={StaticResource boolToVisibilityConverter}}">

        <!-- Other control elements based on various templates -->

        <Control Margin="20,10" Template="{DynamicResource TotalCostsStatisticTemplate}"/>

    </StackPanel>

</ControlTemplate>

此PhasingStatisticValuesTemplate用于另一个ControlTemplate,然后在DataTemplate中使用,因此ControlTemplates有一个很长的层次结构 -

<ControlTemplate x:Key="PhasingStatisticsTemplate">
    <Grid>
        <ScrollViewer Style="{DynamicResource CompactHorizontalScrollViewerStyle}" Name="ScrollContainer">
            <Grid Name="ScrollViewerGrid" Background="Transparent">
                <ContentControl Template="{DynamicResource PhasingStatisticValuesTemplate}" Name="ScrollViewerContent" />
            </Grid>
        </ScrollViewer>
    </Grid>
</ControlTemplate>

另一个有用的信息。可能是该TextBlock上的现有样式也更新了Foreground,不确定它是如何导致此行为的 -

<Style x:Key="PhasingValueTextStyle" TargetType="{x:Type TextBlock}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsComplete}" Value="False">
            <Setter Property="Foreground" Value="{DynamicResource ManagerErrorBrush}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

0 个答案:

没有答案