3

この記事を見てきましたが、列挙値を設定に保存する際に問題が発生します。

次の列挙型を作成しました

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
     }
4

1 に答える 1

1

設定の保存を妨げていると思われる2つの問題を除いて、コードは問題なく表示されます。

  • のを指定する必要があると思いDataContextますRadioButton。Window1を次のように変更するだけです。

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

    (注:StaticResource機能しない場合は、を使用してみてくださいDynamicResource

  • 第二に、あなたの投稿から、あなたはstring設定のように値を保存しているようです。これを変更し、代わりにのデータ型をに設定FileTypeFtypeます。(2がこれを行う方法がわからない場合は、教えてください)

これらの2つの変更を行った後、これは確実に機能します。私は願います ;)

于 2010-01-30T16:53:51.103 に答える