Monday, October 5, 2009

WPF #6 - Creating a RoutedEvent

Just to keep my posting average posting new tips, this is an easy one with an example on how to make a Routed event.

A routed event is just like a regular event, but it has one other great functionality, it can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event (via MSDN).

public class MyButtonSimple: Button
{
// Create a custom routed event by first registering a RoutedEventID
// This event uses the bubbling routing strategy
public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
"Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple));

// Provide CLR accessors for the event
public event RoutedEventHandler Tap
{
add { AddHandler(TapEvent, value); }
remove { RemoveHandler(TapEvent, value); }
}

// This method raises the Tap event
void RaiseTapEvent()
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(MyButtonSimple.TapEvent);
RaiseEvent(newEventArgs);
}
// For demonstration purposes we raise the event when the MyButtonSimple is clicked
protected override void OnClick()
{
RaiseTapEvent();
}
}

Source: MSDN RoutedEventArgs examples


Friday, October 2, 2009

WPF #5 - Styling using MultiTrigger (MultiCondition setter)

Hi again, I noticed that I'm not explaining my tips that much, I wonder if this is happening due to the fact that I've only posted quick tips or if it is an actual issue.

Well, I'm more an example guy than a theory guy, for me is always easy to understand using examples than any other way. So if anyone read one of the tips and didn't understand it, please leave a comment so I can try to explain it better.

Now, this post tip, how to use MultiTrigger (or MultiConditions setters) in WPF, the following example pretty much explains itself:

<Style targettype="{x:Type Button}">
...
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="Content" Value="{x:Null}" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="Yellow" />
</MultiTrigger>
</Style.Triggers>
</Style>

In this code I'm setting the background of a Button only when the mouse is over and the Content is not filled, don't ask me why I'm doing that, it's just an example, probably taken by the source url and used for something else!

Source: WPF Styles and Control Templates

Leave a comment if you guys have any questions.


Thursday, October 1, 2009

DOT.NET - Make invalid UTF8 charatcters available in XML

To change my posts a little lets talk about .NET, not WPF, a .NET little trick I discovered when creating UTF8 enabled XML files.

If you want to enable UTF8 files through an editor you would have to do the following:

<?xml version="1.0" encoding="ISO-8859-1"?>

And to do the same when you use a XmlWriter (that's the trick), you first have to set the Encoding of the XmlWriter and then you just have to force it to write the start document element.

XmlTextWriter objWriter = new XmlTextWriter(sPath, Encoding.UTF8);
objWriter.WriteStartDocument();

Hope it helps!