views:

14

answers:

1

I'm trying to solve the following problem on a WPF datagrid:

I've templated the DataGrid items to contain a button - when clicked, it displays the RowDetails for the grid. I'm having trouble getting that button styled the way I want. Specifically, I want it to appear as a "hyperlink" with a small plus-sign icon next to it. When clicked to display the RowDetails, that icon should change to a minus-sign.

Here's the ControlTemplate I'm attempting to apply to the Button:

<ControlTemplate x:Key="linkButtonTemplate" TargetType="{x:Type Button}">
     <StackPanel Orientation="Horizontal">                    
          <Image x:Name="expanderIcon" Source="Images/Plus.png"/>
          <TextBlock Foreground="Blue" TextDecorations="Underline" Padding="5 0 0 0">
               <ContentPresenter />
          </TextBlock>                    
     </StackPanel>
</ControlTemplate>

So, when the button is clicked, I want to change that Image's Source property. And when it's clicked again, I want to change it back. I've come across the following suggested solutions, and none seem ideal:

1. Use a Trigger on the Button.IsPressed event: It works! ...Except the image reverts once the event ends. (i.e. once the IsPressed property is no longer true). So it doesn't work.

2. Use an EventTrigger: These at least let you catch the Button's Click event... and

Unlike Trigger, EventTrigger has no concept of termination of state, so the action will not be undone once the condition that raised the event is no longer true.

So, great! But they appear to only let you respond with Storyboard actions, not a simple property Setter. Unless I'm missing something, these are not meant for this scenario.

3. Use an Attached Behavior: I'm currently delving into this option, but I just can't help thinking there ought to be a XAML-contained means of doing this. If not, so be it - I'll learn Attached Behaviors.

But my hope is that someone has an angle on this that I haven't encountered.

For example, is there a way to hook the property of the DataRow containing the button that indicates if the RowDetails are currently viewable? Seems like you'd need to crack open the code behind to accomplish that, too... Am I wrong?

+1  A: 

You could use a retemplated ToggleButton for a scenario like this and bind the images visibilities to the IsChecked Property. IT would be also much easier to bind the Visibility of the RowDetails to that.

MrDosu
Ah, good call! Using an alternative to the standard `Button` didn't occur to me. Thanks.
djacobson