- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试基于数据触发器切换 contentpresenter 的内容。我想在 contentpresenter.content 中显示用户控件,如果我设置了值,否则我需要显示错误消息。但是绑定(bind)我的数据触发失败,表明未找到该属性。我无法获取要继承的数据上下文以进行数据触发检查。我可以通过使用注释掉的代码使其工作。但我很困惑为什么它不能按正常方式工作。
<ContentPresenter.Style>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Content" Value="{Binding UC}"/>
<Style.Triggers>
<!--<DataTrigger Binding="{Binding DataContext.HasValue,RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" Value="false">
<Setter Property="Content" Value="No preview"/>
</DataTrigger>-->
<DataTrigger Binding="{Binding HasValue}" Value="false">
<Setter Property="Content" Value="No value"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentPresenter.Style>
</ContentPresenter>
最佳答案
如果你想使用触发器来显示UserControl,你应该使用ContentControl而不是ContentPresenter。我更喜欢将 ContentPresenter 用于自定义控件,当我使用 UserControl 来查看系统中的自定义数据类型的 View 时,并允许提供动态行为。
示例:要切换 ContentPresenter 的模板,您需要像这样设置 ContentTemplateSelector
<ContentPresenter Content="{Binding MyContent}"
ContentTemplate="{Binding MyContentTemplate}"
ContentTemplateSelector="{Binding MyContentTemplateSelector}"/>
MyContent、MyContentTemplate 和 MyContentTemplateSelector 是依赖属性,可以在您使用其实例的任何位置进行绑定(bind)。
阅读:
What is the difference between ContentControl and ContentPresenter
问题中提到的绑定(bind)将不起作用
ContentPresenter’s DataContext is automatically set to the value of its Content property, while ContentControl’s DataContext is not.
绑定(bind)是相对于 DataContext 属性的值来解析的。如果您在 ContentPresenter 上声明绑定(bind),则在设置其内容时,将重新评估该绑定(bind)。
ContentControl.Content 属性可以根据您的要求在任何触发器上进行更改。如果您想使用它来更改 ViewModel 属性的 PropertyChanged 事件,可以通过将其与包含 UserControl 实例的 DataTemplate 绑定(bind)或使用该 UserControl 的静态资源来使用 DataTrigger。
<ContentControl>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Value="{StaticResource UnSelectedDataTemplate}" Property="ContentTemplate" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected}" Value="True">
<Setter Value="{StaticResource SelectedDataTemplate}" Property="ContentTemplate" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentContro.Style>
</ContentControl>
阅读 How to use triggers for content template ,更多详情here
DataTemplate 和 StaticResource 范围的区别在于 DataTemplate 每次应用时都会创建模板的新实例。而 StaticResource 再次使用相同的 UserControl 实例(静态实例)。您还可以使用 EventTriggers 更改基于控制事件(如 MouseOver 等)的内容。
替代方法
与上面非常相似,但略有不同。定义为资源中的数据模板。内容更改的触发本质上是相同的。
...在 <x.Resources />
标签:
<DataTemplate x:Key="DesignerTemplate" DataType="{x:Type vm:SolutionViewModel}">
<vw:SolutionDesignerView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:SolutionViewModel}">
<ContentControl Content="{Binding }">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsLoaded}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource DesignerTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
...然后:
<ContentControl Content="{Binding Solution}" />
关于wpf - contentpresenter.content 上的数据触发不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9359364/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!