views:

218

answers:

2

In a WPF application, I have correctly bound a DataTemplate to an XML node that looks like:

<answer answer="Tree", correct="false" score="10" />

In my application, I have a TextBlock with the answer in it. At first, I want it invisible, but when the correct attribute in the XML file changes to "true", it must become visible.

My DataTemplate is hooked up correctly, because everything else works. For example, if I change the answer attribute in the XML file (just for testing), it changes in my WPF view. But I'm having troubles with the visibility. This is my XAML:

<TextBlock Text="{Binding XPath=@answer}" Visibility="Hidden">
    <TextBlock.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding XPath=@correct}" Value="true">
                    <Setter Property="TextBlock.Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

I'm guessing the Databinding in the DataTrigger isn't working correctly. Anyone have a clue?

A: 

I have run into the same problem with databound ToggleButtons. Try removing the Visibility="False" and replacing it with another DataTrigger that handles the incorrect case.

Dave
+1  A: 

I think the issue is that the Visibility property is hard-coded. Try setting the Visibility in the style:

<TextBlock Text="{Binding XPath=@answer}"> 
    <TextBlock.Style> 
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Visibility" Value="Hidden"/>
            <Style.Triggers> 
                <DataTrigger Binding="{Binding XPath=@correct}" Value="true"> 
                    <Setter Property="TextBlock.Visibility" Value="Visible" /> 
                </DataTrigger> 
            </Style.Triggers> 
        </Style> 
    </TextBlock.Style> 
</TextBlock> 
Tim
Wouldn't he want it as a data trigger instead? What if the answer is changed and is incorrect again? Not sure why a user would do that but it is still something to consider.
Dave
@Dave, you make a good point. My answer is just in response to his particular question and code sample.
Tim