事件设置器 - Visual Studio设计器中的XAML错误

3

我已经用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文件”。

我该怎么办?谢谢! Pileggi

3个回答

2

看起来这是一个已知的 bug。你可以通过将具有 EventSettersStyle 移动到主要的 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>

谢谢!我喜欢这个论坛。它是我从未找到的最好的。它拯救了我的(艰难)生活。 - undefined

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实例中的调试器连接到失败的设计师。不要忘记设置在所有异常上中断。这可能会提供更多关于实际发生情况的见解。
附注2:如果没有任何帮助,你可以使用附加行为来达到相同的结果。

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 Professional 2008 SP1 DataContext是一个包含2个ObservableCollection的类 DataTemplate在Window.Reference中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接