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!


Tuesday, September 29, 2009

WPF #4 - Scroll to a ListViewItem programatically

Hi guys, now you have to be tired of WPF tips. Aren't you?

Well, after playing around a little bit with ListView I've bumped into a problem, how to select an item programatically?

Couldn't be easier:

this.listView.ScrollIntoView(this.listView.Items[index]);

Pretty neat right?

See ya next time!


Friday, September 25, 2009

WPF #3 - Get ListViewItem from its data bound

Another WPF tip, aren't you guys tired of them yet?

Well, this is another quick one, how to get an ListBoxItem or ListViewItem when you have the item that was bound to it.

ListViewItem objItem = myListView.ItemContainerGenerator.ContainerFromItem(objMyData);

Pretty easy right? Or as we brazilians say: fácil!


Thursday, September 24, 2009

WPF #2 - How to set focus and bring windows to front

Hi guys,
I was developing a WPF application that should open a window after clicking tray icon and bumped with a problem when focusing, The same method would be called whether the window was open or not, so the easiest way I've found to do that is the following:
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
private void BringToFront()
{
    if (!IsVisible)
        Visibility = Visibility.Visible;
    SetForegroundWindow((new System.Windows.Interop.WindowInteropHelper(this)).Handle);
}

Tuesday, September 22, 2009

WPF #1 - How to get windows handle

Hi guys,

My first post at my not so recently created blog, Between APIs!
Here I intend to discuss and talk only about my programming daily issues, tips, problems, solutions. If it matter for a developer than its posted here!

My first one is all about starting WPF.
I've stumbled with this problem when starting a new WPF library that would be used by a WindowsForm application. How to get the current WF window handle to use as owner?

Very simple:
MyWpfWindow myWindow = new MyWpfWindow();
WindowInteropHelper windowHelper = new WindowInteropHelper(myWindow).Handle;
windowHelper.Owner = myOldWfForm.Handle;
myWindow.ShowDialog();

Source:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a62f912f-a28e-416b-b0f1-065ae9d6cc01

Hope you guys enjoy the blog!