views:

41

answers:

2

I'm overriding the styles of my ListBoxItems with a ControlTemplate, however by doing that, I lost the handler for my ListBoxItem click event. I found a post that was helpful in saying I need to add an event handler in the ControlTemplate, but I don't know how to do this.

Any help & direction on doing this is greatly appreciated!

A: 

Is it really the mouse click you're after, or are you just responding to a change in selection? If so, you may want to use ListBox.SelectionChanged instead.

Otherwise I believe it's as simple as adding an OnClick=... in the template; the sender will be the element which got clicked.

Lunivore
A: 

ListBoxItem doesn't have a "click" event, so it is not clear what you were doing or what functionality you lost when you added the ControlTemplate.

If you have a button in your ControlTemplate you can set its Click event exactly the same way as you would outside the ContolTemplate. Here's a simple example where ListBoxItem does nothing other than show a Button beside the content, and that button calls an event handler named "OnClickMeButtonClicked":

<Style TargetType="ListBoxItem">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ListBoxItem">
        <DockPanel>
          <Button Content="ClickMe" Click="OnClickMeButtonClicked" />
          <ContentPresenter />
        </DockPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

If what you meant is you want your ListBoxItem to display differently depending on whether the item is selected or not, just set a trigger on IsSelected:

<ControlTemplate TargetType="ListBoxItem">
  <Border Name="Bd">
    <ContentPresenter />
  </Border>

  <ControlTemplate.Triggers>
    <Trigger Property="IsSelected" Value="true">
      <Setter TargetName="Bd" Property="Background" Value="Blue" />
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>
Ray Burns
The first code block is what I want. I defined a ControlTemplate for my ListBoxItems but have no definition for the actual controls - just styles. Just for my knowledge, if I were to leave it like this, would that cause problems? I've been forced to dive head-first into WPF, so just trying to learn/figure out as much as I can as I go.
TheGeekYouNeed
I didn't understand the question you posed in your comment. What does it mean, precisely, to "have no definition for the actual controls - just styles." Which "actual controls?" By "definition" do you mean ControlTemplate or something else?
Ray Burns
Yes I meant in my ControlTemplate. I'm only defining styles, no definition for my content. Like in your code block, your ContentTemplate has <DockPanel><Button>....<DockPanel> within it. Mine does not.
TheGeekYouNeed
If you leave your ControlTemplate empty, every control it applies to will be invisible. This is probably not what you want, so I would have to say that yes, it would "cause problems". But they won't be hidden problems - you will actually see things missing from your UI.
Ray Burns
Strange, they aren't missing - however, I will definitely include them in the ControlTemplate. Thank you so much for your help
TheGeekYouNeed
One more question - am I able to create a Click event for the DockPanel like you do in the Button above? My goal is to open up a new window when a ListBoxItem is clicked.
TheGeekYouNeed
No, DockPanel doesn't have a Click event but it does have a MouseLeftButtonDown event which will probably do the trick for you.
Ray Burns