在设置中存储单选按钮选择

发布于 2024-08-19 23:28:46 字数 3384 浏览 7 评论 0原文

我一直在查看这篇文章,但遇到了问题将枚举值保存在设置中。

我创建了以下枚举,

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

单选按钮选择运行良好,但我现在想在设置中存储所选选项,但似乎无法存储枚举变量。

我以为我可以将枚举转换为字符串,然后再转换回来,但是当谈到 WPF 时,我有点菜鸟,我不太确定从哪里开始。

这是我到目前为止生成的代码:

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
     }

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
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2024-08-26 23:28:46

您的代码看起来很好,除了我认为可能阻止您存储设置的两个问题:

  • 我认为您应该为 RadioButton 指定一个 DataContext。只需像这样修改您的 Window1:

    
        >;
        >;
        >;
    
    

    (注意:如果 StaticResource 不起作用,请尝试使用 DynamicResource

  • 其次,从您的帖子来看,您似乎将值存储为设置中的字符串。只需更改此设置并将 FileType 的数据类型设置为 Ftype 即可。 (如果您不知道如何做到这一点,请告诉我)

完成这 2 项更改后,您一定会成功!我希望;)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文