9
I'm trying to use an attached property to trigger a style change on a UIElement when an event has fired.
Here is the case scenario:
A user sees a TextBox, and focuses then unfocuses it. Somewhere in an attached property it notices this LostFocus event and sets a property (somewhere?) to say that it HadFocus.
The style on the TextBox then knows that it should style itself differently based on this HadFocus property.
Here's how I imagine the markup to look...
<TextBox Behaviors:UIElementBehaviors.ObserveFocus="True">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Behaviors:UIElementBehaviors.HadFocus" Value="True">
<Setter Property="Background" Value="Pink"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
I've tried a few combinations of the attached properties to get this working, my latest attempt throws a XamlParseException stating "Property can not be null on Trigger."
public class UIElementBehaviors
{
public static readonly DependencyProperty ObserveFocusProperty =
DependencyProperty.RegisterAttached("ObserveFocus",
typeof (bool),
typeof (UIElementBehaviors),
new UIPropertyMetadata(false, OnObserveFocusChanged));
public static bool GetObserveFocus(DependencyObject obj)
{
return (bool) obj.GetValue(ObserveFocusProperty);
}
public static void SetObserveFocus(DependencyObject obj, bool value)
{
obj.SetValue(ObserveFocusProperty, value);
}
private static void OnObserveFocusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)