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!