2010-06-08 65 views
2

我已经使用XAML完成了我的TreeView,但是现在我想用代码隐藏来管理事件。 HierarchicalDataTemplate包含一个Image。我需要捕获图像上的事件MouseEnter/MouseLeave。我已经试过这样:EventSetter - Visual Studio设计器中的错误XAML

<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的设计师出现错误:“无法加载文件XAML与EventSetter”。

我该如何补救? 谢谢! Pileggi

回答

2

它看起来这是一个known bug。您可以通过简单地用EventSetters移动Style到主Resources范围,包括它在你的DataTemplate作为StaticResource来解决它:

<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> 
+0

谢谢!!我喜欢这个论坛。这是我从未找到的最好的。它节省了我的(艰难)生活。 – lamarmora 2010-06-08 16:02:41

0

请问您能否提供更多的上下文?我无法重现你的错误在VS 2008中,有以下简单的XAML:

<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中有什么?您的数据模板位于何处?你如何参考它?注意:您也可以尝试使用另一个Visual Studio实例附加失败的设计器和调试器。不要忘记设置break on all exceptions。这可能会提供更多的真知灼见。

PPS:如果没有什么帮助,你可以用attached behavior来达到同样的结果。

0

非常感谢你,如果我的信息不够,我感到抱歉! 这是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专业版2008 SP1 DataContext的是2的ObservableCollection 一类的DataTemplate在Window.Reference

相关问题