1

Hallo i have a problem with an eventsetter.
My Window:

<TreeView.Resources>
    <ResourceDictionary> 
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary 
                 Source="CrefoChartTreeViewItemStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <HierarchicalDataTemplate 
            DataType="{x:Type local:Node}" 
            ItemsSource="{Binding ChildNodes}">
        </HierarchicalDataTemplate>
    </ResourceDictionary>
</TreeView.Resources>

My CrefoChartTreeViewItemStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
mc:Ignorable="d"
>
    <Style TargetType="TreeViewItem">
        <Style.Resources>
            <LinearGradientBrush x:Key="ButtonBrush" EndPoint="0,1" StartPoint="0,0">
                <GradientStop Color="White" Offset="0.25"/>
                <GradientStop Color="#FFA5DBE9" Offset="1"/>
            </LinearGradientBrush>
            <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
                <Setter Property="Background" Value="{DynamicResource ButtonBrush}" />
                <EventSetter Event="Click" Handler="ButtonOnClick" />
            </Style>
        </Style.Resources>  

I get the Error Message when i Compile:

The event 'click' can not be specified on a Target tag in a Style. Instead, use "EventSetter".

What do i do wrong?

Is there any other way to get this button in the treeview triggerd? so i can put Code behind?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72

1 Answers1

1

This won't work, Because a resource xaml can't have a code behind file, they are usually called "loose xaml". You can read about that in the msdn about EventSetter. What you can and should do is to use something that converts your events to commands, like the AttachedCommandBehavior this works very nicely together with MVVM. If you want to use events like you asked for, you can place the TreeView in a UserControl, and then you can use events.

dowhilefor
  • 10,971
  • 3
  • 28
  • 45
  • 1
    I disagree - resource dictionary xamls *can* have a code-behind file. You can read about it in [this SO question](http://stackoverflow.com/questions/92100/is-it-possible-to-set-code-behind-a-resource-dictionary-in-wpf-for-event-handlin) and in [this SO question](http://stackoverflow.com/questions/7045718/wpf-events-in-resourcedictionary-for-a-controltemplate) (and probably more). – O. R. Mapper Dec 16 '12 at 13:54
  • @O.R.Mapper ok you are right, if you go that far it is possible. But i would say its not the regular case. – dowhilefor Dec 16 '12 at 19:16