如何从TabItem的DataTemplate中找到ItemTemplate

时间:2013-01-05 22:24:03

标签: wpf xaml binding triggers datatemplate

我正在尝试在DataTemplate中设置触发器,以便在源对象的属性HasUnreadMessages设置为true时,标题文本变为红色。但是从datatemplate中的触发器内部,我不知道如何引用标头的TextBlock

完整代码:

<Window x:Class="IRC.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:loc="clr-namespace:IRC"
        Title="" Height="350" Width="525">

    <Window.Resources>
        <DataTemplate DataType="{x:Type loc:Tab}">
            <DockPanel>
                <ListBox x:Name="lstUsers" ItemsSource="{Binding Users}" Visibility="Collapsed" DockPanel.Dock="Right" />
                <ListBox x:Name="lstMessage" ItemsSource="{Binding Messages}" />
            </DockPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Type}" Value="Channel">
                    <Setter TargetName="lstUsers" Property="Visibility" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding HasUnreadMessages}" Value="True">
                    <Setter TargetName="tabHeader" Property="Foreground" Value="Red" /> // -- ERROR
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Window.Resources>

    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_Actions">
                <MenuItem Header="_Connect" InputGestureText="Alt+C" Click="Connect" />
            </MenuItem>
        </Menu>
        <TextBox Name="txtInput" Height="22" VerticalContentAlignment="Center" SpellCheck.IsEnabled="True" DockPanel.Dock="Bottom" />
        <TabControl Name="pnlTabs" ItemsSource="{Binding}" ContentTemplate="{Binding}" DockPanel.Dock="Top">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock x:Name="tabHeader" Text="{Binding Name}"/>
                </DataTemplate>
            </TabControl.ItemTemplate>
        </TabControl>
    </DockPanel>
</Window>

错误:

The name "tabHeader" is not recognized. 
The member "Foreground" is not recognized or is not accessible. 

标签:

public class Tab
{
    public string Name { get; set; }
    public string Type { get; set; }
    public ObservableCollection<string> Users { get; set; }
    public ObservableCollection<string> Messages { get; set; }
    public bool HasUnreadMessages { get; set; }
}

1 个答案:

答案 0 :(得分:0)

您正在尝试更改此时未知的对象的属性。

而不是将DataTrigger放在Tab上,将DataTrigger移动到TabItem - 因为它是您尝试更改的TabItem ...

在您的情况下,添加TabItem样式:

<Window.Resources>
 <DataTemplate DataType="{x:Type TabItem}" x:Key="ItemTemplateStyle">
            <TextBlock x:Name="tabHeader" Text="{Binding Name}"/>
        <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding HasUnreadMessages}" Value="True">
            <Setter TargetName="tabHeader" Property="Foreground" Value="Red" />
        </DataTrigger>
        </DataTemplate.Triggers>
 </DataTemplate>

并为TabItems设置此样式:

<TabControl Name="pnlTabs" ItemsSource="{Binding}" ContentTemplate="{Binding}" DockPanel.Dock="Top" 
            ItemTemplate="{StaticResource ItemTemplateStyle}" />