3

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

我创建了以下枚举

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

我的单选按钮选择工作得很好,但我现在想将选定的选项存储在设置中,但似乎没有存储枚举变量的能力。

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

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

应用程序.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>

应用程序.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
     }
4

1 回答 1

1

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

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

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

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

  • 其次,从您的帖子看来,您将值存储string在设置中。只需更改此设置,而是将数据类型设置FileTypeFtype。(如果你不知道 2 如何做到这一点,请告诉我)

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

于 2010-01-30T16:53:51.103 回答