【问题标题】:Storing a radio button selection in the settings在设置中存储单选按钮选择
【发布时间】:2017-01-31 07:00:01
【问题描述】:

我一直在查看 this article,但在保存设置中的枚举值时遇到问题。

我创建了以下枚举

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>

转换器.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
     }

【问题讨论】:

    标签: wpf enums radio-button application-settings


    【解决方案1】:

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

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

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

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

    • 其次,从您的帖子看来,您在设置中将值存储为string。只需更改它并将FileType 的数据类型设置为Ftype。 (如果你不知道 2 是怎么做到的,请告诉我)

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

    【讨论】:

    • 我不确定如何更改设置属性“FileType”的类型,因为设置设计器仅在列表中提供标准类型,而 FType 是我自己的枚举,它不存在。我可以看到如何修改 Settings.Designer.cs 可以编辑,但这是一个自动生成的文件,将被覆盖。在我存储选定的选项之前,我不知道静态或动态资源是否有效(它与两个选项一起编译,所以它一定可以工作。;o)谢谢你的帮助。
    • 好的..这样做:在Visual Studio中右键单击您的项目>>属性>>设置。在这里你有你的FileType 财产,对吧?现在在 Type 下拉列表中,选择 Browse 并手动输入 [namespace].[class] 格式的枚举。你完成了!否则,您也可以手动编辑文件:不是Settings.Designer.cs(如您所说,它是自动生成的),而是Settings.settings 文件。这不是自动生成的。希望这会有所帮助:)
    • 米希尔。你是绝对的明星!谢谢。
    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 2022-12-11
    • 2018-03-08
    • 2012-12-30
    相关资源
    最近更新 更多