From: Chris L on
I want to have a custom control that changes its border brush color to the
selected background brush color at IsMouseOver trigger event. How can I do
this?

<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" TargetName="border" Value="{Binding
Background}"/>
</Trigger>
</ControlTemplate.Triggers>

Regards,
Chris
From: Peter Duniho on
Chris L wrote:
> I want to have a custom control that changes its border brush color to the
> selected background brush color at IsMouseOver trigger event. How can I do
> this?
>
> <ControlTemplate.Triggers>
> <Trigger Property="IsMouseOver" Value="True">
> <Setter Property="BorderBrush" TargetName="border" Value="{Binding
> Background}"/>
> </Trigger>
> </ControlTemplate.Triggers>

It would be helpful if you were more specific. Posting just the Trigger
you're creating provides no context at all, such as what the rest of the
ControlTemplate looks like, how you're applying the template, etc. And
of course your question implies that the above doesn't work for you, but
you don't say in what way. Are there errors? Debug output messages?
Incorrect output? All of the above?

That said, here's some XAML I managed to get working. I don't know if
it's the best way to accomplish what you're doing; for one, it doesn't
use a ControlTemplate…instead, I used a Style because I'm unfamiliar
with the ControlTemplate (that's where a complete code example from you
would have helped). I also wish I knew a way to set the DataContext for
the whole Style or Triggers collection so that the RelativeSource didn't
have to be specified for every binding you want.

But I'm far from being a WPF expert, so this is what you get. :)

It _does_ set the border of the control to which I've applied the style
to the same color as the background. Which seems to me what you're
trying to do.

I will note that I found that when my Label control had a BorderBrush
explicitly set for the control, that overrode the Style's values, both
for initialization and for the trigger. Apparently the Style can't set
the property if it's explicitly set in the control itself.

If you find a better way to accomplish this, please follow-up with a
post illustrating it (with a complete code example like the one I've
posted here).

Pete


<Window x:Class="TestWPFTriggerBorderBrush.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">

<Window.Resources>

<Style x:Key="MyLabelStyle" TargetType="Label">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="AliceBlue" />
</Setter.Value>
</Setter>
<Setter Property="BorderBrush">
<Setter.Value>
<SolidColorBrush Color="DodgerBlue" />
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="{Binding
Path=Background, RelativeSource={x:Static RelativeSource.Self}}" />
</Trigger>
</Style.Triggers>
</Style>

</Window.Resources>

<Grid>
<Label Style="{StaticResource MyLabelStyle}" Content="Test
Label" Height="51" HorizontalAlignment="Left" Margin="12,12,0,0"
Name="label1" VerticalAlignment="Top" BorderThickness="5" Width="112"
FontSize="20" />
</Grid>

</Window>