I have ListBox that is supposed to show a list of Fields. When empty, I wanted to have inside the ListBox a "Add Field" button. So, I created a Style for the empty case that adds the button. I needed to add a handler for the Click event for the button to create a new Field and add it to the list, so I used an EventSetter to attach the handler. Here is the code that I have.
<ListBox.Style>
<Style TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}">
<Style.Triggers>
<Trigger Property="HasItems" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Button Content="Add field" Height="20">
<Button.Style>
<Style TargetType="{x:Type Button}">
<EventSetter Event="Click" Handler="AddField_Click" />
</Style>
</Button.Style>
</Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Style>
The problem is that when I compile, I get the following error on the line where the EventSetter tag is:
error MC4007: The event 'Click' cannot be specified on a Target tag in a Style.
Use an EventSetter instead.
I am stumped. I already use an EventSetter. How do I resolve this? Is there some other way of specifying the EventSetter that I should use?
Try creating a
Style
and assign that toButton
. Like,This should work.