使用multiDataTrigger时出现TextBlock异常

时间:2011-07-18 07:53:02

标签: wpf xaml multidatatrigger

我正在尝试向文本块的visibility属性添加2个触发器。 以下代码导致“TextBlock异常”。知道为什么吗?

<TextBlock Name="tb2"
           Style="{StaticResource MyPageTextStyle}"
           Text="{x:Static res:MyResources.MyText}"
           Visibility="Collapsed"
           >
           <TextBlock.Triggers>
                    <MultiDataTrigger>
                           <MultiDataTrigger.Conditions>
                                    <Condition Binding="{BindingPath=Class1.IsBlue}" Value="true" />
                                    <Condition Binding="{Binding Path=Class1.IsGreen}" Value="false" />
                           </MultiDataTrigger.Conditions>
                           <Setter TargetName="tb2" Property="TextBlock.Visibility" Value="Visible" />
                    </MultiDataTrigger>
            </TextBlock.Triggers>
 </TextBlock>

1 个答案:

答案 0 :(得分:1)

不是向textblock添加触发器,而是将触发器添加到textblock样式......有些如何直接向控件添加触发器会抛出xaml异常......

尝试做这样的事......

<TextBlock Name="tb2" Visibility="Collapsed">
            <TextBlock.Style>
                <Style>
                     <Style.Triggers>
                        <MultiDataTrigger>
                               <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding Path=Class1.IsBlue}" Value="true" />
                                        <Condition Binding="{Binding Path=Class1.IsGreen}" Value="false" />
                               </MultiDataTrigger.Conditions>
                               <Setter Property="TextBlock.Visibility" Value="Visible" />
                        </MultiDataTrigger>
                     </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>