Archives for : WPF

WP7, ApplicationBar, ImageUri and the Icon not showing up

I know this may sound stupid but when working with the WP7 ApplicationBar make sure the images you are using have the following properties:

Build Action: Content
Build Action: Content
Copy to Output: Copy always (or ‘Copy if newer’, either should work)

I spent a half an hour trying to figure out why my application bar was showing an “X” instead of the image I was putting in IconUri. When first adding images the properties default to:

Build Action: Resource
Copy to Output: Do not copy

Since the application bar is referencing the image by Uri it can’t seem to find it embedded in the application.

My xaml looks like:

    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">

WPF – ContentProperty, it’s that simple

So there I am, creating a custom control called “RequiredLabel” that
interestingly enough is a label that has a cool icon in it to show that
whatever it is being used for is required. The control has two
properties, Text that is the text of the label and Style that is the
style of the label. Both are registered as dependency properties so
that they can be set in the xaml. I suppose it would have been fine to
simply leave good enough as good enough but I wanted it so that in the
xaml you can put the text for the label between the start and end tag
for required label. It is simply expected behavior the the content of
your control can be put in that way.

After much soul searching (i.e. Googling) I still couldn’t find the
answer. How do I make one of my dependency properties the content that
is between the tags? Well, thankfully I had an epiphany and remembered
that somewhere deep in one of my WPF books the answer was held. It is
as simple as putting the attribute above the class declaration with the
dependency property name.

public partial class RequiredLabel : UserControl
    //see full code below

This makes it so that when I use my custom control it will look like:

<customControls:RequiredLabel x:Name="lblType" Style="{StaticResource LabelGradient}">
    Defendant's Name:

It also makes it so that if I decide to extend the content to support
user controls instead of just text really easy since I can switch out
the TextBlock that makes up the user control to a panel.

Also if you want to get a glimmer of using dependency properties see
attached code.


WPF and Silverlight

 Check out the link below.  It is a bunch of tutorials on everything you could want to know about Silverlight plus a whole bunch of cool WPF stuff.

WPF and MousePosition

The way I thought it should be done (but it’s wrong):

Point mousePoint = Mouse.GetPosition(this);
MyWindow win = new MyWindow();
win.Left = mousePoint.X;
win.Top = mousePoint.Y;

“Mama Freeda! Dang window won’t go where I put it!”

Okay, maybe I didn’t say “Mama Freeda” nor “Dang” but none the less it
seems like when getting a mouse position in WPF and trying to open a new
window relative to the mouse click is a pain.

Ah Luigi, but I have the solution 🙂

When setting the left and top properties in a wpf window they relate to
the desktop coordinates. The new window could careless that it’s being
popped up from a control underneath it.

The problem is that Mouse.GetPosition(this) returns the position
relative to the “this” which is most likely the control that is trying
to open the new window. Now if the control is the only control in the
window, there is only one screen, the application is running on the
primary screen and the application is maximized then your golden.

But screw that.

Well, how about using Mouse.GetPosition(Window.GetWindow(this))?

That returns the point relative to the window of the control (this).
That should work won’t it? That only eliminates a single concern of
ours, if the control is the only control in the window. There is still
that the application must be only one screen, the application is running
on the primary screen and the application must be maximized.

Brian, cut the Brother Stuart and just give us the simple answer!

Um, who’s Brother Stuart?

And why am I talking to myself like this?

The great thing about wpf controls is that they have the ability given a
point to convert that point to screen (i.e. desktop) coordinates.

So here is easy answer (The way it should be done):

Point mousePoint = this.PointToScreen(Mouse.GetPosition(this));
MyWindow win = new MyWindow();
win.Left = mousePoint.X;
win.Top = mousePoint.Y;

Basically get the mouse position relative to this control and then
convert it out to the desktop coordinates so I can then use it when
setting the left and top properties of a new window. This is great for
pop-up windows that need to be close to a mouse click. The great thing
about this is that since the mouse click position is now relative to the
desktop this works on multi-screen monitors regardless of which monitor
you have the application running on.

Later ‘yall,