我尝试在Hyperlink
上使用Style
对象设置Foreground
颜色,但其没有任何效果。我甚至使用Changing Hyperlink foreground without losing hover color的BasedOn
提示,但它没有什么区别 - 我仍然会看到一个蓝色超链接,它在悬停时显示为红色。如何将样式传播到DataTemplate中的超链接?
下面是我的控制XAML,包括ItemsControl
其项目使用超链接显示:
<StackPanel Background="Red" TextElement.Foreground="White">
<StackPanel.Resources>
<Style TargetType="Hyperlink" BasedOn="{StaticResource {x:Type Hyperlink}}">
<Setter Property="Foreground" Value="Yellow"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBlock>Data import errors</TextBlock>
<ItemsControl ItemsSource="{Binding Errors}"/>
</StackPanel>
而且在ItemsControl
项目被拾起以下DataTemplate
:
<DataTemplate DataType="{x:Type Importer:ConversionDetailsMessage}">
<TextBlock>
<Run Text="{Binding Message, Mode=OneTime}"/>
<Hyperlink Command="Common:ImportDataCommands.ExplainConversionMessage" CommandParameter="{Binding}">
<Run Text="{Binding HelpLink.Item2, Mode=OneTime}"/>
</Hyperlink>
</TextBlock>
</DataTemplate>
这是值得注意的是,我不想直接在DataTemplate
的Hyperlink
上设置不同的颜色。这是因为模板将被使用不同的ItemsControl
对象,其中大多数对象将在白色背景上,因此可以使用标准的超链接颜色。 (注意,上面的XAML中的那个背景是红色的。)
总之,我不希望DataTemplate
必须知道任何有关使用它的控件。模板控件的样式应该过滤到它。
所以......任何人都可以告诉我为什么风格没有过滤掉,我能做些什么来解决它?
谢谢。
更新:
因为我不能让帕夫洛的答案在我的生产应用程序的工作,因为我已经尝试过在一个单独的测试程序。该应用程序是一个WinForms应用程序,主窗体仅包含一个ElementHost
,它本身包含一个简单的WPF用户控件。下面是它的XAML:“只能与目标类型风格是基本类型‘IFrameworkInputElement’基地”
<UserControl x:Class="DataTemplateStyling.StylingView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:DataTemplateStyling="clr-namespace:DataTemplateStyling"
x:Name="root" Loaded="StylingViewLoaded">
<UserControl.Resources>
<Style x:Key="MyDefaultHyperlinkStyle" BasedOn="{StaticResource {x:Type Hyperlink}}"/>
<DataTemplate DataType="{x:Type DataTemplateStyling:ImportMessage}">
<DataTemplate.Resources>
<Style TargetType="{x:Type Hyperlink}"
BasedOn="{StaticResource MyDefaultHyperlinkStyle}"/>
</DataTemplate.Resources>
<TextBlock>
<Run Text="{Binding Message, Mode=OneTime}"/>
<Hyperlink NavigateUri="{Binding HelpLink.Item1}">
<Run Text="{Binding HelpLink.Item2, Mode=OneTime}"/>
</Hyperlink>
</TextBlock>
</DataTemplate>
</UserControl.Resources>
<Grid DataContext="{Binding ElementName=root}">
<StackPanel Background="Red" TextElement.Foreground="White">
<StackPanel.Resources>
<Style x:Key="MyDefaultHyperlinkStyle" TargetType="Hyperlink" BasedOn="{StaticResource {x:Type Hyperlink}}">
<Setter Property="Foreground" Value="Yellow"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBlock>Data import errors</TextBlock>
<ItemsControl ItemsSource="{Binding Messages}"/>
</StackPanel>
</Grid>
</UserControl>
正如上述表示,这将产生一个InvalidOperationException
,说明
可以通过将TargetType="Hyperlink"
置于UserControl.Resources
元素内的Style
定义上来修复这个问题。然而,当正在显示的信息,他们的链接部分仍然有默认的蓝色超链接样式:
总之,它不工作,所以我欢迎任何其他建议/更正。 :(
更新2:
感谢来自帕夫洛替代的解决方案,现在是工作:)
令人敬畏的谷歌搜索技巧 - 谢谢!还有一个非常完整的答案。我会试试这个,并将其标记为接受后,我有它的工作。 – 2011-03-03 17:47:30
忘记我的应用程序是使用'ElementHost'的WinForms应用程序,所以我没有app.xaml文件。尽管如此,原理看起来很合理,所以我试着在我的UserControl资源字典中的DataTemplate之前添加一个空的MyDefaultHyperlinkStyle,并留下上面看到的特定的StackPanel以用它自己的定义替换它。可悲的是,它仍然无法正常工作。我会继续尝试...... – 2011-03-03 21:20:19
@Mal - 你忘了在数据模板本身中包括样式:' '? –
2011-03-03 21:33:17