在 XAML 中指定数据上下文会阻止数据触发器工作
我有一个 DataTrigger 设置,如下所示:
<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>
以及两个使用它的控件:
<TextBlock Style="{StaticResource resultTypeStyle}" FontSize="14" Grid.Row="2">Condition</TextBlock>
<myns:ConditionBuilderView Style="{StaticResource resultTypeStyle}" DataContext="{Binding Condition}" Grid.Row="3"/>
结果类型由组合框更改。
当结果类型为“Confirmation”时,TextBox
消失,但 ConditionBuilderView
保持可见除非我从其中删除 DataContext
属性它。
我需要设置 DataContext
属性,以便控件获取正确的数据。
设置数据上下文意味着可见性属性不再起作用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上正在更改 ConditionBuilderView 样式中用于绑定的上下文。因此,您的 DataTrigger 在绑定条件上查找 Result.Path。如果它只是关于可见性,这应该有效:
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: