views:

342

answers:

1

I've been looking at this article but am having issues saving the enumerated value in the settings.

I have created the following enum

public enum FType
{
    None,
    Delimited,
    FixedWidth,
    XML
};

I have the radio button selection working nicely but I now want to store the selected option in the settings but there doesn't appear to be the ability to store an enumerated variable.

I assumed I could convert the enum to a string and then convert back but being a bit of a noob when it comes to WPF I'm not realy sure where to start.

Here is the code I've generated so far: App.Xaml

<Application x:Class="Widget.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:properties="clr-namespace:Widget.Properties"
    StartupUri="Window1.xaml"
    Exit="Application_Exit">
    <Application.Resources>
        <properties:Settings x:Key="Settings" />
    </Application.Resources>
</Application>

App.xaml.cs

public partial class App : Application { private void Application_Exit(object sender, ExitEventArgs e) { Widget.Properties.Settings.Default.Save(); } }

Windows.xaml

<Window x:Class="Widget.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Widget"
    Title="Window1" Height="85" Width="300">
    <Window.Resources>
        <local:EnumBooleanConverter x:Key="enumBooleanConverter"/>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <RadioButton GroupName="FileType" Content="Delimited"   IsChecked="{Binding Path=Default.FileType, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Delimited}" />
            <RadioButton GroupName="FileType" Content="Fixed Width" IsChecked="{Binding Path=Default.FileType, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=FixedWidth}"/>
            <RadioButton GroupName="FileType" Content="XML"         IsChecked="{Binding Path=Default.FileType, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=XML}"/>
        </StackPanel>
    </Grid>
</Window>

Converter.cs

    public class EnumBooleanConverter : IValueConverter
    {
        public EnumBooleanConverter()
        {
        }

        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string parameterString = parameter as string;
            if (parameterString == null)
                return DependencyProperty.UnsetValue;

            if (Enum.IsDefined(value.GetType(), value) == false)
                return DependencyProperty.UnsetValue;

            object parameterValue = Enum.Parse(value.GetType(), parameterString);

            return parameterValue.Equals(value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string parameterString = parameter as string;
            if (parameterString == null)
                return DependencyProperty.UnsetValue;

            return Enum.Parse(targetType, parameterString);
        }
        #endregion
     }
A: 

Your code looks just fine, except 2 problems that I think may be preventing you from storing settings:

  • I think you should specify a DataContext for your RadioButtons. Just modify your Window1 like this:

    <StackPanel DataContext="{StaticResource Settings}">
        <RadioButton GroupName=... />
        <RadioButton GroupName=... />
        <RadioButton GroupName=... />
    </StackPanel>
    

    (Note: If StaticResource doesn't work try using DynamicResource)

  • Secondly, from your post it seems that you are storing values as string in settings. Just change this and instead set datatype of FileType to Ftype. (If you don't know how 2 do this, tell me)

After doing these 2 changes you'll surely get this working! I hope ;)

Mihir Gokani
I'm not sure how to change the type for the Settings property "FileType" as the settings designer only provides standard types in the list and as FType is my own enum, it isn't there.I can see how to modify the Settings.Designer.cs could be edited but this is an auto generated file and will be overwritten.Until I get the selected option stored I won't know if Static or Dynamic resource works (it compiles with both options so It's bound to work. ;o)Thanks for your help.
TeamWild
Ok.. Do this way: In visual studio Right click your project >> Properties >> Settings. Here you have your `FileType` property, right? Now in `Type` dropdown, select `Browse` and manually type your enum in [namespace].[class] format. And you're done! Otherwise you can also manually edit the file : not the `Settings.Designer.cs` (which is auto generated - as you said) but the `Settings.settings` file. This isn't auto generated. Hope this helps :)
Mihir Gokani
Mihir. You are an absolute star! Thanks.
TeamWild