我已经用XAML完成了我的TreeView,但是现在我想用代码隐藏来管理一个事件。HierarchicalDataTemplate包含一个图像。我需要捕获映像上的事件MouseEnter / MouseLeave。我试过这样做:
<Image x:Name="imgArticolo" Source="{Binding imgArt}">
<Image.Style>
<Style TargetType="{x:Type Image}">
<EventSetter Event="MouseEnter" Handler="iArt_MouseEnter"/>
</Style>
</Image.Style>
</Image> 但在Visual Studio的设计器中出现错误:“无法使用EventSetter加载文件XAML”。
我该怎么补救呢?谢谢!皮莱基
发布于 2010-06-08 23:48:22
看起来这是一个known bug。只需将带有EventSetters的Style移到主Resources作用域中,并将其作为StaticResource包含在DataTemplate中,就可以解决这个问题
<Style x:Key="myImageStyle" TargetType="{x:Type Image}">
<EventSetter Event="MouseEnter" Handler="iArt_MouseEnter"/>
</Style>
<HierarchicalDataTemplate x:Key="modTreeArtDataParts2">
<Grid>
<Border x:Name="bdArt">
<Image x:Name="imgArticolo" Source="{Binding imgArt}" Height="Auto"
Style="{StaticResource myImageStyle}" />
</Border>
</Grid>
</HierarchicalDataTemplate>发布于 2010-06-08 17:13:34
你能提供更多的背景信息吗?我不能用以下简单的XAML在VS2008中重现你的错误:
<Window x:Class="WpfWindow.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<HierarchicalDataTemplate x:Key="template"
ItemsSource="{Binding Children}">
<Image x:Name="imgArticolo"
Source="{Binding imgArt}">
<Image.Style>
<Style TargetType="{x:Type Image}">
<EventSetter Event="MouseEnter"
Handler="iArt_MouseEnter" />
</Style>
</Image.Style>
</Image>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<TreeView ItemTemplate="{StaticResource template}">
<TreeViewItem Header="Hey" />
</TreeView>
</Grid>
</Window>您使用的是什么版本的Visual Studio?DataContext中有什么?您的数据模板位于何处?你怎么指代它?
PS:您也可以尝试从Visual Studio的另一个实例中使用调试器附加到失败的设计器。别忘了设置break on all exceptions。这可能会让我们对那里真正发生的事情有更多的了解。
PPS:如果没有什么真正的帮助,你可以使用attached behavior来达到同样的效果。
发布于 2010-06-08 21:31:12
非常感谢,如果我的信息不充分,非常抱歉!这是XAML代码(清除了所有与之无关的内容),没有被拒绝的行,它工作得很好。
<TreeView x:Name="tvArt"
ItemTemplate = "{DynamicResource modTreeArtDataParts}"
ItemsSource = "{Binding RicambiList, Source={StaticResource P_RicambiDataSource}}"/>
<HierarchicalDataTemplate x:Key="modTreeArtDataParts"
ItemsSource = "{Binding RicambiItemList}"
ItemTemplate="{StaticResource modTreeArtDataParts2}">
<Grid>
...
</Grid>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="modTreeArtDataParts2">
<Grid>
<Border x:Name="bdArt">
<Image x:Name="imgArticolo" Source="{Binding imgArt}" Height="Auto">
<!-- refused rows -->
<Image.Style>
<Style TargetType="{x:Type Image}">
<EventSetter Event="MouseEnter" Handler="iArt_MouseEnter"/>
</Style>
</Image.Style>
</Image>
</Border>
</Grid>
</HierarchicalDataTemplate>我使用Visual Studio Professional 2008 SP1,DataContext是一个有2个ObservableCollection的类,DataTemplate在Window.Reference中
https://stackoverflow.com/questions/2995730
复制相似问题