WPF: Creating a Pop Up Button Style

August 7th, 2009 by Mel 3 comments »

So I’ve been getting pretty fancy with my GUI development, and today I wanted to design a button that would appear as just flat text until you hovered over it with the mouse, at which point it would “pop up” and look like an actual button.

Regular Button'Flat' Button

The following solution only works when run on a system with a Windows Vista/7 theme enabled, such as Aero or Basic. It will not work when run with the Windows Classic theme.

The ‘Flat’ Pop Up Button was achieved by simply setting the background and border to transparent. Here’s the XAML:

<Button Content="Click Me!" Background="Transparent" BorderBrush="Transparent"/>

The next step was to make it so that when the mouse hovers over the button it “pops up” visually and looks like your standard button. At first I tried several methods for accomplishing this using triggers to set the style of the button which were all turning out to be pretty messy since I’m not the best graphical designer.

But silly me, I didn’t realize that in this case I didn’t have to do anything special at all! I could just leave the button as it is in the XAML above and the baked in triggers for the Button would still kick in and change the appearance when the mouse hovered over it.

Here’s what that looks like:

Pop Up Button

It looks pretty cool in spots where you want a nice, Aero style button, but if the user isn’t interacting with it you don’t want all those pixels distracting from the clean whiteness that is your polished application.

WPF: Setting a Type Specific Property Value

August 7th, 2009 by Mel 5 comments »

While XAML in WPF is probably the most powerful GUI development architecture I’ve ever used and by far my favorite, there’s still a few little tasks here and there that I just scratch my head trying to figure out.

This is probably due to my limited knowledge of XAML and how to use it properly, so until I get it all mastered I’m forced to use lines of DuctTape to get some of the results I want.

Here’s my most recent hack job. If you look at the XAML below you’ll notice that I’m making use of the Tag property of the button. This is just an example, but in real life there’s actually quite a few valuable uses for Tag.

<Button Content="Am I the right button?" Tag="False" Click="Button_Click" />
<Button Content="Am I the right button?" Tag="True" Click="Button_Click" />
<Button Content="Am I the right button?" Tag="False" Click="Button_Click" />

So in this example the user needs to click the right button. The right button is indicated by its tag property. In this case the second button is the one that works, right?

Which button is it?

Not exactly.

The code to handle this situation would look something like ths:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var sourceBtn = sender as Button;
 
    bool isRight = (bool)sourceBtn.Tag;
}

Since Tag is defined as an object and I’m binding it to “False” in my XAML I thought this code would work out just fine, but it doesn’t.

The problem is Tag gets populated with a string instead of a boolean. That’s interesting, after all why shouldn’t it? How is XAML supposed to know that what I wanted to set to Tag is the boolean value False? It doesn’t.

So I had to write my XAML in such a way that it knew without a doubt that I was using a boolean value.

The first thing in accomplishing this was to include the System namespace in my XAML file, like so:

xmlns:system="clr-namespace:System;assembly=mscorlib"

Then I could declare my Tag property full out giving it a system:Boolean for its value.

<Button Content="Am I the right button?" Click="Button_Click">
	<Button.Tag>
		<system:Boolean>False</system:Boolean>
	</Button.Tag>
</Button>
<Button Content="Am I the right button?" Click="Button_Click">
	<Button.Tag>
		<system:Boolean>True</system:Boolean>
	</Button.Tag>
</Button>
<Button Content="Am I the right button?" Click="Button_Click">
	<Button.Tag>
		<system:Boolean>False</system:Boolean>
	</Button.Tag>
</Button>

And that’s it, it works! Yay!

But man, it really is ugly. Does anyone know of a cleaner, better way to accomplish the same thing? I would love to learn something new!

WPF: Bind Control Enabled to Checkbox Checked

July 23rd, 2009 by Mel 5 comments »

This is a simple example of a nifty use of binding in WPF.

Say you have an element in your application such as a TextBox, ComboBox, or some RadioButtons that you want to keep disabled to the user unless the user checks a box. You may even want to disable a whole section of controls contained inside of a StackPanel or DockPanel.

Everything enabled

To accomplish this you bind the IsEnabled property of the target control (in this case a ComboBox) to the IsChecked property of the CheckBox.

XAML:

<StackPanel>
    <CheckBox x:Name="chkEnableBackup">Run Backup Sets</CheckBox>
 
    <StackPanel Orientation="Horizontal">
        <Label Margin="12,0,0,0">Run backup every</Label>
        <ComboBox Width="70" SelectedIndex="0"
          IsEnabled="{Binding ElementName=chkEnableBackup, Path=IsChecked}">
            <ComboBoxItem>Minute</ComboBoxItem>
            <ComboBoxItem>Hour</ComboBoxItem>
            <ComboBoxItem>Day</ComboBoxItem>
        </ComboBox>
    </StackPanel>
</StackPanel>

That’s it! Our result is that when the box is not checked the control is disabled.

Control disabled!

WPF: Center Child Window

May 7th, 2009 by Mel 2 comments »

Here’s a method I’ve found for centering a window to either its parent or the main window for the application, in WPF. It’s not too far different from how you do it in WinForms.

For the child window set it’s WindowStartupLocation to “CenterOwner.” This will cause it to show in the center of the owning Window.

<Window x:Class="WpfApplication1.TestChild"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TestChild" Height="300" Width="300"
 
    WindowStartupLocation="CenterOwner">

Now all that’s left to do is set its owner before displaying it. If the code you’re using to display the window is running inside of a Window class, then you can just use this.

TestChild testWindow = new TestChild();
testWindow.Owner = this;
testWindow.Show();

This isn’t always the case however, sometimes you need to display the child window from code running on a page or a user control. In this case you want the child window to be centered to the main window of the application.

TestChild testWindow = new TestChild();
testWindow.Owner = Application.Current.MainWindow;
testWindow.Show();

C#: Detect If You’re Running on the Server

April 27th, 2009 by Mel No comments »

If you ever need to detect whether your code is running on the machine that you’re talking to through IP (a server application for example), and you know the IP address for the remote machine, then this code may come in handy. It was originally written by my buddy Erin for a project we both worked on.

using System.Net;
 
private bool CheckIfServer(IPAddress serverIP)
{
    // Get all addresses assigned to this machine
    List<IPAddress> ipAddresses = new List<IPAddress>();
    ipAddresses.AddRange(Dns.GetHostAddresses(Dns.GetHostName()));
 
    // If desirable, also include the loopback adapter
    ipAddresses.Add(IPAddress.Loopback);
 
    // Detect if this machine contains the IP for the remote server
    // Note: This uses a Lambda Expression, which is only available .Net 3.x
    return ipAddresses.Exists(i => i.ToString() == serverIP.ToString());
}

If you don’t know the IP Address for your remote server you can easily get it using the server’s host name like this:

IPAddress[] addresses = Dns.GetHostAddresses("remote_host_address");

This returns an IPAddress[] which includes all the resolved addresses for that host.