Storing a radio button selection in the settings

。_饼干妹妹 提交于 2019-12-24 00:24:39

问题


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
     }

回答1:


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 ;)



来源:https://stackoverflow.com/questions/2163025/storing-a-radio-button-selection-in-the-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!