开发者

Specifying the data context in XAML stops the data trigger from working

I have a DataTrigger setup like so:

<UserControl.Resources>
    <Style x:Key="resultTypeStyle">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Result, Path=Result.Type}" Value="Confirmation">
                <Setter Property="Control.Visibility" Value="Collapsed" />
            </DataTrigger>
   开发者_如何学编程     </Style.Triggers>
    </Style>
</UserControl.Resources>

And two controls that use it:

    <TextBlock Style="{StaticResource resultTypeStyle}" FontSize="14" Grid.Row="2">Condition</TextBlock>
    <myns:ConditionBuilderView Style="{StaticResource resultTypeStyle}" DataContext="{Binding Condition}" Grid.Row="3"/>

The result type is changed by a combo box.

When the result type is Confirmation then the TextBox disappears but the ConditionBuilderView stays visible unless I remove the DataContext attribute from it.

I need to set the DataContext attribute so that the control gets the right data.

What does setting the data context do that means the visibility property no longer works?


You are actually changing the context used for the binding in the style of the ConditionBuilderView. So your DataTrigger looks for Result.Path on the bound Condition. If its just about Visibility this should work:

<TextBlock Style="{StaticResource resultTypeStyle}" FontSize="14" Grid.Row="2">Condition</TextBlock>
<ContentControl Grid.Row="3" Style="{StaticResource resultTypeStyle}" >
    <myns:ConditionBuilderView DataContext="{Binding Condition}"/>
</ContentControl>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜