DataTemplate触发器不起作用

时间:2012-03-20 15:49:21

标签: wpf triggers datatemplate

我有这段简单的代码,但是没有用。我们的想法是标签页眉中的图标会根据视图模型的内容而改变。

<DataTemplate DataType="{x:Type ProtocolViewModels:ProtocolTabItemContentViewModel}">
    <TabItem>
        <TabItem.Header>
            <Controls:TabItemHeader x:Name="tabItemHeader" 
                Header="{Binding TabHeader}" 
                Icon="{DynamicResource ProtocolCustom_TabIcon}"
                CloseCommand="{Binding CloseCommand}"/>
        </TabItem.Header>
        <LegacyProtocolViews:ProtocolTabItemContentView/>
    </TabItem>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding ProtocolViewModel.IsDakoOrigin}" Value="True">
            <Setter TargetName="tabItemHeader" Property="Icon" Value="{DynamicResource ProtocolDako_TabIcon}"/>
        </DataTrigger>  
    </DataTemplate.Triggers>
</DataTemplate>

视图模型上的属性通知视图有关更改。这不应该起作用吗?

修改

TabItemHeader.xaml.cs

    public static readonly DependencyProperty IconProperty =
        DependencyProperty.Register("Icon", typeof(BitmapImage), typeof(TabItemHeader), new PropertyMetadata(default(BitmapImage)));

    public BitmapImage Icon
    {
        get { return (BitmapImage)GetValue(IconProperty); }
        set { SetValue(IconProperty, value); }
    }

TabItemHeader.xaml

<UserControl x:Class="Workstation.Resources.Controls.TabItemHeader"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"
             x:Name="persistingTabItem">
    <!-- skipped code -->
    <Image Source="{Binding ElementName=persistingTabItem, Path=Icon}"/> 
    <!-- skipped code -->
</UserControl>

1 个答案:

答案 0 :(得分:0)

只是一个猜测。 Binding="{Binding ProtocolViewModel.IsDakoOrigin}" - &gt; Binding="{Binding IsDakoOrigin}"因为看起来IsDakoOrigin是模板的DataContext的属性。您还可以检查输出窗口,然后调试应用程序的绑定错误