Tuesday, June 8, 2010
WPF #7 - Several Tips
Hope you enjoy it.
1 - How to programmatically change the SelectedItem in a WPF TreeView
2 - Popup tracking position of PlacementTarget
3 - WPF Tutorial - Resizeable Popup | Switch on the Code
4 - How to: Position a Popup
5 - WPF UserControl in DataTemplate within ItemsControl - how to bind to parent
6 - WPF: Binding to Properties in your UserControl or Window
7 - How do you override the opacity of a parent control in WPF?
8 - WPF Button IsEnabled Based on ComboBox Selection Overwriting default style
That's pretty much it.
See ya!
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
[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);
}Hope this helps!
Source:
http://www.dotnetspider.com/resources/5772-Bring-e-window-Front-set-It-Active-window.aspx