SyntaxHighlighter

Wednesday, November 16, 2011

More on Binding an Enum to radiobutton

I was playing around with my program and tried adding a group of four radio buttons. I found that the second group interfered with the first. So I put the second group in a stackpanel, and everything worked! One more thing, in my isClicked method I didn't show that I have this:
if (skipcheck)
{
   return;
}


where skipcheck is a bool that is set to true until the initialization is done. This stops an exception. This is a very nice method to use, and easy.

Tuesday, November 15, 2011

Binding an Enum to radiobuttons

I have a WPF project that uses radio buttons to exclusively choose between three options. There was a lot of code to decide which radio was pushed. Thanks to a blog post by Todd Davis, I got binding working on the radio buttons.
Suppose you have an Enum that corresponds to your radio buttons, and then set up a dependency property on it.

public enum KmlFileTypes
{
Simple,
Slider,
Splits
}

public static readonly DependencyProperty MkftProperty =
DependencyProperty.Register("Mkft",
typeof(KmlFileTypes),
typeof(MainWindow));

public KmlFileTypes Mkft
{
get { return (KmlFileTypes)GetValue(MkftProperty); }
set { SetValue(MkftProperty, value); }
}

The binding works because of a enum to boolean converter:
public class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value.ToString() == parameter.ToString());
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Enum.Parse(targetType, parameter.ToString(), true) : null;
}
}

then all you have to do is declare the radio buttons in the xaml:

RadioButton
Margin="20,20,0,0"
Content="Simple Path"
IsChecked="{Binding Mkft, ElementName=Utils, ConverterParameter=Simple, Converter= {StaticResource enumToBool}, Mode=TwoWay}"
Checked="isClicked"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="80"
Height="24" />

This is just one of the three radio buttons, but you will notice that there is no group involved, and you do not need to name them.

The code for isClicked is:

private void isClicked(object sender, RoutedEventArgs e) //get the radiobutton that was pushed
{

textBox2.Text = ((int)Mkft).ToString(); //display the radio button name

Properties.Settings.Default.KmlFileType = (int)Mkft; //save the radio button selection
}

If you want to have one of the buttons pushed, based on user settings, do this in your initialization:

Mkft = (KmlFileTypes)Properties.Settings.Default.KmlFileType;


This couldn't be easier, and it just works!

Tuesday, October 4, 2011

User Feedback

I have contacted several people to see if they want to be beta testers for my program. I have not heard back so far. The program lets you see where you went on your bicycle in Google Earth, if you were using a bike computer with a GPS unit in it. Actually, I have used the program to track walks, kayaking, and bike riding. Maybe today I will hear of any interest in the program

Friday, September 16, 2011

Fixed a problem

I had a problem in the program that I use to generate Google Earth KML files. I wanted to add a user name, but I needed to learn about WPF validation to make sure the user entered a name. That was harder than it looks. I had previously done validation on a Windows form, but WPF is different. After quite a bit of searching on Google, and a question to Stackoverflow, I got it to work. Now my program asks once for the user to enter her/his name, and ever after remember that name, and uses it. I love working with WPF, but sometimes it makes you wonder what Microsoft was thinking. I love being able to view the resulting KML file on my smartphone.

Monday, September 5, 2011

More on the Garmin to KML conversion

This has been a very interesting program to write. I used C#, and WPF to create a program to display the input file from my Edge 800, and be able to see the data. The ability to write KML was just the finishing touch.

Sunday, September 4, 2011

Seeing my bike ride in Google Earth


Visualizing bicycle rides in Google Earth

I started using a Garmin Edge 800 to track my bicycle rides. The Training Center software works very well, and shows me a simple map of my ride. I saw that other people imported their rides into Google Earth, and I wondered how they did it. I learned that Google Earth uses Keyhole Markup Language (KML) to map things. This is what I wanted. I knew C#, and so decided to write a program to display my bike rides in Google Earth. After a short learning curve, I now have a program to read the export file from Garmin Training Center (tcx) and outputs a kml file. I have three options to write the file, a simple path, an animated path, and a path with splits displayed. This has been a fun project, and now I can see where I went on a very good map.