我有這段簡單的代碼不起作用。這個想法是,選項卡標題中的圖標將根據視圖模型的內容進行更改。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>
你可以顯示你的代碼和xaml控件:TabItemHeader和圖標資源。我嘗試了類似的datatrigger,一切正常。您還可以嘗試通過datatrigger更改TabItemHeader的頭文件,以查看數據觸發器或圖標是否存在問題 – Nikolay 2012-03-21 08:28:33
添加的代碼示例。與此同時,我通過使用ValueConverter來解決這個問題。我更喜歡數據觸發器。 – Michael 2012-03-22 18:56:45