none
multidatatrigger is not working

    Question

  • Hello All,

    Could you please tell me the problem with the following code? its not working.. I just want to change the visibility of textblock on some conditons.

     <TextBlock x:Name="timeoutMessage" Text=" (timeout)" HorizontalAlignment="Left" Visibility="Collapsed">
                                        <TextBlock.Style>
                                            <Style>
                                                <Style.Triggers>
                                                    <MultiDataTrigger>
                                                        <MultiDataTrigger.Conditions>
                                                            <Condition Binding="{Binding Path=IsComplete}" Value="false"></Condition>
                                                            <Condition Binding="{Binding DataContext.TimeoutExpired, ElementName=timeoutMessage}" Value="true"></Condition>
                                                        </MultiDataTrigger.Conditions>
                                                        <Setter Property="TextBlock.Visibility" Value="Visible"></Setter>
                                                    </MultiDataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </TextBlock.Style>
                                    </TextBlock>


    Monday, October 01, 2012 1:40 PM

Answers

  • Hi,

    The reason your trigger does not appear to be working is because of WPF's value resolution strategy.

    see this link and scroll down -

    http://www.wpftutorial.net/dependencyproperties.html

    Basically a style setter has a higher priority than a trigger so your visibility=collapsed is always going to override your trigger.

    To get around this I would remove the visibility=Collapsed (making it visible) and then change the trigger so that when the corrdcvt values are presnt the tigger sets the visiblity of the textBlock to collapsed.

    In the example below I inverted the values of your trigger (but just change it to what works correctly for your sample -

    <TextBlock x:Name="timeoutMessage" Text=" (timeout)" HorizontalAlignment="Left">
                                        <TextBlock.Style>
                                            <Style>
                                                <Style.Triggers>
                                                    <MultiDataTrigger>
                                                        <MultiDataTrigger.Conditions>
                                                            <Condition Binding="{Binding Path=IsComplete}" Value="True"></Condition>
                                                            <Condition Binding="{Binding DataContext.TimeoutExpired, ElementName=timeoutMessage}" Value="False"></Condition>
                                                        </MultiDataTrigger.Conditions>
                                                        <Setter Property="TextBlock.Visibility" Value="Collapsed"></Setter>
                                                    </MultiDataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </TextBlock.Style>
                                    </TextBlock>

    Hope that helps?

    • Marked as answer by pratikmehta9 Monday, October 01, 2012 3:32 PM
    Monday, October 01, 2012 2:53 PM

All replies

  • I have tried following also(after changin the way to access Datacontext), but still not working:

    <TextBlock x:Name="timeoutMessage" Text=" (timeout)" HorizontalAlignment="Left" Visibility="Collapsed">
                                        <TextBlock.Style>
                                            <Style>
                                                <Style.Triggers>
                                                    <MultiDataTrigger>
                                                        <MultiDataTrigger.Conditions>
                                                            <Condition Binding="{Binding Path=IsComplete}" Value="False"></Condition>                                                       
                                                            <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.TimeoutExpired}" Value="True"></Condition>
                                                        </MultiDataTrigger.Conditions>
                                                        <Setter Property="TextBlock.Visibility" Value="Visible"></Setter>
                                                    </MultiDataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </TextBlock.Style>
                                    </TextBlock>


    Monday, October 01, 2012 2:47 PM
  • Hi,

    The reason your trigger does not appear to be working is because of WPF's value resolution strategy.

    see this link and scroll down -

    http://www.wpftutorial.net/dependencyproperties.html

    Basically a style setter has a higher priority than a trigger so your visibility=collapsed is always going to override your trigger.

    To get around this I would remove the visibility=Collapsed (making it visible) and then change the trigger so that when the corrdcvt values are presnt the tigger sets the visiblity of the textBlock to collapsed.

    In the example below I inverted the values of your trigger (but just change it to what works correctly for your sample -

    <TextBlock x:Name="timeoutMessage" Text=" (timeout)" HorizontalAlignment="Left">
                                        <TextBlock.Style>
                                            <Style>
                                                <Style.Triggers>
                                                    <MultiDataTrigger>
                                                        <MultiDataTrigger.Conditions>
                                                            <Condition Binding="{Binding Path=IsComplete}" Value="True"></Condition>
                                                            <Condition Binding="{Binding DataContext.TimeoutExpired, ElementName=timeoutMessage}" Value="False"></Condition>
                                                        </MultiDataTrigger.Conditions>
                                                        <Setter Property="TextBlock.Visibility" Value="Collapsed"></Setter>
                                                    </MultiDataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </TextBlock.Style>
                                    </TextBlock>

    Hope that helps?

    • Marked as answer by pratikmehta9 Monday, October 01, 2012 3:32 PM
    Monday, October 01, 2012 2:53 PM
  • Thank you very much Pritesh, its working now..
    Monday, October 01, 2012 3:32 PM