views:

192

answers:

3

Hi!

I've done my TreeView all with XAML but now I'd like to manage an event with code-behind. The HierarchicalDataTemplate contains an Image. I need to capture the events MouseEnter / MouseLeave on the Image. I've tried in this way:

<Image x:Name="imgArticolo" Source="{Binding imgArt}"> 
    <Image.Style> 
        <Style TargetType="{x:Type Image}"> 
            <EventSetter Event="MouseEnter" Handler="iArt_MouseEnter"/> 
        </Style> 
    </Image.Style> 
</Image> 

But in the designer of Visual Studio appear the error: "Impossible to load a file XAML with EventSetter".

How can I remedy? Thank you! Pileggi

A: 

Hey Pileggi!

Could you please provide a little more of the context? I couldn't reproduce your error in VS 2008, with the following simple 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>

What version of Visual Studio do you use? What is in the DataContext? Where is your data template located? How do you refer to it?

PS: You can also try attaching to failing designer with debugger from another instance of Visual Studio. Don't forget to set break on all exceptions. This may give more insights on what's really happening there.

PPS: If nothing really helps you can use attached behavior to achieve the same result.

Anvaka
A: 

Thank you very much and sorry if my information were insufficient! This is the XAML code (cleaned of everything that has nothing to do), without the refused rows it works well.

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

I use Visual Studio Professional 2008 SP1 The DataContext is a class with 2 ObservableCollection The DataTemplate is in the Window.Reference

pileggi
A: 

It looks like this is a known bug. You may be able to resolve it by simply moving the Style with EventSetters to the main Resources scope and including it in your DataTemplate as a 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>
Joseph Sturtevant
Thank you!! I love this forum. It's the best I never found. It saves my (hard) life.
pileggi