[英]DataTemplate Based on Value of Bound Property in ItemsSource of TabControl
Ok. 好。 So I have a
TabControl
object in my xaml that has an ItemsSource
value of ItemsSource={Binding OpenTabs}
where OpenTabs
is an ObservableCollection
of type ClosableTab
( public ObservableCollection<ClosableTab> OpenTabs { get; set; }
) which extends TabItem
. 所以,我有一个
TabControl
在我XAML对象,其具有ItemsSource
的值ItemsSource={Binding OpenTabs}
其中OpenTabs
是一个ObservableCollection
类型的ClosableTab
( public ObservableCollection<ClosableTab> OpenTabs { get; set; }
其延伸TabItem
。 I found ClosableTab
from here and then have adapted it's view for my own needs. 我从这里找到
ClosableTab
,然后根据自己的需要调整了它的视图。
Primarily I have added a property (and sorry for the confusion in names here) isProperty
. 最初,我添加了一个属性(抱歉,这里的名称不正确)
isProperty
。 This is for a real estate program. 这是针对房地产计划的。 Then in my xaml I have the following lines:
然后在我的xaml中,我有以下几行:
<DataTemplate x:Key="PropertyTemplate">
<Grid>
<TextBlock Text="{Binding address}"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="TennantTemplate">
<Grid>
<TextBlock Text="{Binding name}"/>
</Grid>
</DataTemplate>
//... That's in <Windows.Resources>
<TabControl ItemsSource="{Binding OpenTabs}" Grid.Column="1" x:Name="Tabs">
<TabControl.Resources>
<DataTemplate x:Key="DefaultTab">
<ContentControl>
<ContentControl.Triggers>
<DataTrigger Binding="{Binding isProperty}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource PropertyTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding isProperty}" Value="False">
<Setter Property="ContentTemplate" Value="{StaticResource TennantTemplate}" />
</DataTrigger>
</ContentControl.Triggers>
</ContentControl>
</DataTemplate>
</TabControl.Resources>
</TabControl>
I've done some research and found that this is what I need to do if I want to have a certain DataTemplate
dependant on the property in ClosableTab
called isProperty
. 我做了一些研究,发现这是我需要做的,如果我想有一定
DataTemplate
依赖于财产ClosableTab
称为isProperty
。
It's not giving me what I want. 它没有给我我想要的东西。 Can someone please explain to me what I'm doing wrong here?
有人可以告诉我我在做什么错吗? And tell me what I should do?
告诉我该怎么办? And/or possibly give me an alternative method?
和/或可能给我另一种方法? I can't think of what I need to change to get the functionality that I need.
我想不出需要更改什么才能获得所需的功能。 Thanks in advance.
提前致谢。
You need to set DataType
on DataTemplate
to get it applied automatically to underlying data objects in case you are defining DataTemplate
under Resources section. 如果要在“资源”部分下定义
DataTemplate
,则需要在DataTemplate
上设置DataType
使其自动应用于基础数据对象。
<DataTemplate DataType="local:ClosableTab">
<ContentControl>
<ContentControl.Triggers>
<DataTrigger Binding="{Binding isProperty}" Value="True">
<Setter Property="ContentTemplate"
Value="{StaticResource PropertyTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding isProperty}" Value="False">
<Setter Property="ContentTemplate"
Value="{StaticResource TennantTemplate}" />
</DataTrigger>
</ContentControl.Triggers>
</ContentControl>
</DataTemplate>
Make sure to declare local
namespace at root level to the one where ClosableTab
is declared. 确保申报
local
在根级别到一个地方命名ClosableTab
声明。
OR 要么
Instead of adding DataTemplate in resources, set it explicitly as ItemTemplate
of TabControl. 而不是在资源中添加DataTemplate,而是将其显式设置为TabControl的
ItemTemplate
。
<TabControl>
<TabControl.ItemTemplate>
<DataTemplate x:Key="DefaultTab">
.....
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
UPDATE UPDATE
Ideal case would be to have single DataTemplate
and apply dataTrigger on TextBlock instead. 理想的情况是只有一个
DataTemplate
并将dataTrigger应用于TextBlock。
<TabControl ItemsSource="{Binding OpenTabs}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="{Binding address}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding isProperty}"
Value="False">
<Setter Property="Text" Value="{Binding name}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.