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. I found ClosableTab from here and then have adapted it's view for my own needs.

Primarily I have added a property (and sorry for the confusion in names here) isProperty. This is for a real estate program. Then in my xaml I have the following lines:

<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.

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 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.

OR

Instead of adding DataTemplate in resources, set it explicitly as ItemTemplate of TabControl.

<TabControl>
  <TabControl.ItemTemplate>
     <DataTemplate x:Key="DefaultTab">
        .....
     </DataTemplate>
  </TabControl.ItemTemplate>
</TabControl>

UPDATE

Ideal case would be to have single DataTemplate and apply dataTrigger on TextBlock instead.

<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归因
不隶属于 StackOverflow
scroll top