- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下用户控制:
<TabItem
x:Name="Self"
x:Class="App.MyTabItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:App"
>
<TabItem.Header>
<!-- This works -->
<TextBlock Text="{Binding ElementName=Self, Path=ShortLabel, UpdateSourceTrigger=PropertyChanged}"/>
</TabItem.Header>
<TabItem.ContentTemplate>
<DataTemplate>
<!-- This binds to "Self" in the surrounding window's namespace -->
<TextBlock Text="{Binding ElementName=Self, Path=ShortLabel, UpdateSourceTrigger=PropertyChanged}"/>
DependencyProperty
'ShortLabel' 来实现一个接口(interface)。我想从
TabItem
中绑定(bind)到这个属性和其他属性。的
DataTemplate
.但是由于奇怪的交互,
TextBlock
在
DataTemplate
内绑定(bind)到
父容器 的
TabItem
,也称为“Self”,但在另一个 Xaml 文件中定义。
TemplateBinding
: 尝试在 TabItem
的内部绑定(bind)到 ContentPresenter . FindAncestor, AncestorType={x:Type TabItem}
: 找不到 TabItem
parent 。当我指定 MyTabItem
时,这也不起作用类型。 ElementName=Self
: 尝试在错误范围内绑定(bind)到具有该名称的控件(父容器,而不是 TabItem
)。我认为这给出了一个提示,为什么这不起作用:DataTemplate 不是在 XAML 中定义的位置创建的,而是显然是由父容器创建的。 ControlTemplate
达到我正在寻找的效果,但由于我想保留
TabItem
的默认外观和感觉无需维护整个
ControlTemplate
,我很不愿意这样做。
TabControl
s 不能有(任何)
ItemsTemplate
(包括
DisplayMemberPath
)如果
ItemsSource
包含
Visual
s。有
a thread on MSDN Forum explaining why .
最佳答案
问题似乎是您使用的是 ContentTemplate 而没有实际使用 content 属性。 ContentTemplate 的DataTemplate 的默认DataContext 是TabItem 的Content 属性。然而,我所说的都没有真正解释为什么绑定(bind)不起作用。不幸的是,我不能给你一个明确的答案,但我最好的猜测是,这是因为 TabControl 重用了 ContentPresenter 来显示所有选项卡项的内容属性。
因此,在您的情况下,我会将代码更改为如下所示:
<TabItem
x:Class="App.MyTabItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:App"
Header="{Binding ShortLabel, RelativeSource={RelativeSource Self}}"
Content="{Binding ShortLabel, RelativeSource={RelativeSource Self}}" />
<TabItem
x:Class="App.MyTabItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:App"
Header="{Binding ShortLabel, RelativeSource={RelativeSource Self}}"
Content="{Binding ComplexShortLabel, RelativeSource={RelativeSource Self}}">
<TabItem.ContentTemplate>
<DataTemplate TargetType="{x:Type ComplexType}">
<TextBlock Text="{Binding Property}" />
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
关于wpf - 如何从 ContentTemplate 绑定(bind)到周围的自定义控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/148704/
我在 StackPanel 中有一个列表框和一个边框,类似于以下内容:
我有一个 ContentControl,我想在某些事件中更改它的 ContentTemplate。我想在加载 ContentTemplate 中的控件时添加一些值(文本到 TextBox)。 但是,我
假设我使用以下在 XAML 中定义的 ContentTemplate 创建了一个 UserControl: 我如何访问我的代码中的“myEl
在 TabControl.ContentTemplate 中添加用户控件的新实例时,我有点难以接受。 ? 我的 Xaml 在这里:
有人可以将其翻译成 C# 代码: 最佳答案 test.ContentTemplate = (DataTemplate) FindResource("myA"); 关于wpf - 如何以编程方
我希望 ContentTemplate 根据 DataTrigger 中的值而变化。 是的,我考虑过使用 DataTemplateSelector,但现在我需要 DataTrigger 或者更好地说是
我正在尝试为我的 TextBox 实现一个模板,它在 TextBox 的右侧显示一个小图像,就像它可以在 ValidationTemplates 中实现:
我一直有一个问题困扰了我一段时间,但我相信我终于找到了它。症状是,当我的绑定(bind)属性之一触发换出 ContentTemplate 的 DataTrigger 时,我的 WPF 控件将无法正确呈
我正在学习WPF和MVVM模式,并试图建立一个类似日历的 View 。 所以我目前有一个包含6行7列的网格。 第一行应为标题,从而指定“星期一,星期二等...”这样的星期几 我的MonthView.x
我有以下用户控制: 这个自定义 TabItem 定义了一个 D
第一种样式的 xaml 可以按我的意愿工作,使用 setter 生成一个带有 Wingding 字形的按钮来布置内容及其属性。这种风格的第二个版本试图做同样的事情,但使用 DataTemplate 作
我有一个包含 ContentControl 的 UserControl .当用户点击这个 ContentControl我要换它ContentTemplate , 使其“可编辑”(例如,而不是标签显示文
在下面的 Xaml 中,第一个控件(TextBlock 本身)在绑定(bind)和呈现 RecordCount 的值时没有问题。但在第二个控件(带有 ContentTemplate 的 Label)中
我想修改 WPF ListView,以便水平呈现项目,并且第一个项目和所有后续项目之间有一个分隔符。像这样: 我有水平位,但我被分隔符卡住了。我尝试使用 DataTemplate,但这会将分隔符合并到
好像有一个 ContentPresenter在我的 ListBoxItem.ContentTemplate导致 Visual Studio 崩溃?
我在 tabControl 的内容模板的数据绑定(bind)方面遇到了麻烦。 我有那个课 public class MainWindowViewModel : INotifyPropertyChang
好吧...这让我很困惑。我已经在我的 UserControl 子类中重写了 OnContentTemplateChanged 。我正在检查为 newContentTemplate 传入的值实际上等于
我正在尝试拆分我的 View成多个UserControl s 然后显示相应的 View取决于某个值。 我在 WPF 中是这样完成的:
场景: 目前我有这个 XAML 代码: 显然,访问键(“c”键:_Cancel)不能与 TextBlock 结合使用。我实际上认为
ContentControl.Template 和 ContentControl.ContentTemplate 有什么区别?我什么时候使用哪个? 例如,我可以在 WPF 的 xaml 文件中编写:
我是一名优秀的程序员,十分优秀!