首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EventSetter - Visual Studio设计器中的错误XAML

EventSetter - Visual Studio设计器中的错误XAML
EN

Stack Overflow用户
提问于 2010-06-08 16:24:47
回答 3查看 1.6K关注 0票数 2

我已经用XAML完成了我的TreeView,但是现在我想用代码隐藏来管理一个事件。HierarchicalDataTemplate包含一个图像。我需要捕获映像上的事件MouseEnter / MouseLeave。我试过这样做:

代码语言:javascript
复制
<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”。

我该怎么补救呢?谢谢!皮莱基

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-06-08 23:48:22

看起来这是一个known bug。只需将带有EventSettersStyle移到主Resources作用域中,并将其作为StaticResource包含在DataTemplate中,就可以解决这个问题

代码语言:javascript
复制
<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>
票数 2
EN

Stack Overflow用户

发布于 2010-06-08 17:13:34

你能提供更多的背景信息吗?我不能用以下简单的XAML在VS2008中重现你的错误:

代码语言:javascript
复制
<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来达到同样的效果。

票数 0
EN

Stack Overflow用户

发布于 2010-06-08 21:31:12

非常感谢,如果我的信息不充分,非常抱歉!这是XAML代码(清除了所有与之无关的内容),没有被拒绝的行,它工作得很好。

代码语言:javascript
复制
<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中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2995730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档