在设置中存储单选按钮选择 [英] Storing a radio button selection in the settings
问题描述
我一直在看这篇文章,但是我在设置中保存枚举值有问题。
I've been looking at this article but am having issues saving the enumerated value in the settings.
我创建了以下枚举:
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.
我假设我可以将枚举转换为字符串,然后转换回来,但是有点noob当涉及到WPF我不是真的确定从哪里开始。
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
App.xaml.cs
public partial class App : Application
{
private void Application_Exit(object sender, ExitEventArgs e)
{
Widget.Properties.Settings.Default.Save();
}
}
Windows.xaml
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
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
}
推荐答案
您的代码看起来很好,除了我认为可能阻止您存储设置的两个问题:
Your code looks just fine, except 2 problems that I think may be preventing you from storing settings:
-
我想你应该为
RadioButton
指定一个DataContext
。只需修改您的Window1,就像这样:
I think you should specify a
DataContext
for yourRadioButton
s. Just modify your Window1 like this:
<StackPanel DataContext="{StaticResource Settings}">
<RadioButton GroupName=... />
<RadioButton GroupName=... />
<RadioButton GroupName=... />
</StackPanel>
(注意:如果 StaticResource
工作尝试使用 DynamicResource
)
(Note: If StaticResource
doesn't work try using DynamicResource
)
其次,从你的帖子看来,你正在将值存储为 string
在设置中。只需更改此值,而将 FileType
的数据类型设置为 Ftype
。 (如果你不知道2怎么做,请告诉我)
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 ;)
这篇关于在设置中存储单选按钮选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!