Wpf 如何从ContentTemplate绑定到周围的自定义控件?
我有以下用户控件:Wpf 如何从ContentTemplate绑定到周围的自定义控件?,wpf,xaml,binding,datatemplate,Wpf,Xaml,Binding,Datatemplate,我有以下用户控件: <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" > <T
<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”来实现接口。我想从TabItem
的DataTemplate
中绑定到此属性和其他属性。但是由于奇怪的交互作用,DataTemplate
中的TextBlock
被绑定到TabItem
的父容器,该容器也被称为“Self”,但在另一个Xaml文件中定义
问题:
为什么绑定在TabItem.Header中工作,而不是在TabItem.ContentTemplate中工作,我应该如何继续从DataTemplate中获取用户控件的属性
我已经试过了
:尝试在TemplateBinding
的内部绑定到ContentPresenterTabItem
:找不到FindAncestor,AncestorType={x:Type TabItem}
父项。当我指定TabItem
类型时,这也不起作用MyTabItem
:尝试绑定到错误范围内具有该名称的控件(父容器,而不是ElementName=Self
)。我认为这给出了一个提示,为什么这不起作用:DataTemplate不是在XAML中定义的位置创建的,而是由父容器创建的TabItem
ControlTemplate
,以达到我想要的效果,但是由于我想保留TabItem
的默认外观,而不必维护整个ControlTemplate
,所以我非常不愿意这样做
编辑
同时我发现问题是:TabControl
s不能有(任何)ItemsTemplate
(包括displaymberpath
),如果ItemsSource
包含Visual
s。在那里
因为这似乎是WPF的TabControl的一个基本问题,所以我结束这个问题。谢谢你的帮助 试试这个。我不确定它是否有效,但是
<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.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ShortLabel}"/>
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
问题似乎在于,您使用的是ContentTemplate,而没有实际使用content属性。ContentTemplate的DataTemplate的默认DataContext是TabItem的Content属性。然而,我所说的没有一个能真正解释绑定不起作用的原因。不幸的是,我不能给你一个明确的答案,但我最好的猜测是,这是因为TabControl重用ContentPresenter来显示所有选项卡项的content属性 因此,在您的情况下,我会将代码更改为如下所示:
<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}}" />
如果ShortLabel是一个更复杂的对象,而不仅仅是一个字符串,那么您需要生成一个ContentTemplate:
<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>
David Schmitt-您最好将找到的信息作为答案发布并接受它?因为没有理由结束这个问题,答案会很有帮助。@MikroDel:我不想让代表来回答这个问题。您可以自由发布和增强我在问题“编辑”下所写的内容作为答案。这并没有改变WPF不支持这个特定用例的事实。这不是关于rep for answer。答案也可以是否定的——例如这里的“WPF不支持…”。我的意思是,如果信息作为答案而不是问题,其他用户将更容易找到信息。你可以将其作为答案发布并接受它。我不想这样做(作为答案发布),因为我还没有找到这个信息。
<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>