我做了一个单元选择器无线电按钮,“毫米”和“英寸”,让用户切换之间的公制单位和帝国单位。我有两个单选按钮绑定到一个设置文件中的枚举。我用这个作为参考,而且它很好用。在设置中存储单选按钮选择。
我现在需要刷新我的所有属性,这些属性具有我的度量,以反映用户对度量或帝国的偏好。这是在启动,说单位设置为公制,但他们想看看他们是什么帝国。用户选择帝国单选按钮来显示帝国度量,当他们单击单选按钮“英寸”时,所有数据都会刷新,显示将显示在帝国度量衡中,,但是如何在与设置文件中的枚举绑定的单选按钮上更改属性?还是有不同的方法?
如果我不需要存储他们对公制或帝国的偏好,我会切换到一个bool,每个单选按钮,并使用通知属性更改。
编辑想出了答案。我把它作为答案发布了。
发布于 2012-10-02 19:40:38
因此,当我试图让代码的另一部分工作时,我就想出了答案。
我原来的无线电按钮是这样的。
<!--Unit Selector-->
<ribbon:RibbonGroup x:Name="Unit_Selection" Header="Units">
<StackPanel DataContext="{StaticResource Settings}">
<!--Metric-->
<RadioButton GroupName="UnitsSelector" Content="mm" x:Name="Metric" IsChecked="{Binding Path=Default.Units, Mode=TwoWay, Converter={StaticResource enumBooleanConverter},
ConverterParameter=mm}" />
<!--Imperial-->
<RadioButton GroupName="UnitsSelector" Content="inches" x:Name="Imperial" IsChecked="{Binding Path=Default.Units, Mode=TwoWay, Converter={StaticResource enumBooleanConverter},
ConverterParameter=inches}" />
</StackPanel>
</ribbon:RibbonGroup>它将绑定定向到用户设置枚举。
我更改了绑定到一个属性。获取xaml代码
<ribbon:RibbonGroup x:Name="Unit_Selection" Header="Units">
<StackPanel>
<RadioButton IsChecked="{Binding Path=UnitProperty, Converter={StaticResource enumBooleanConverter}, ConverterParameter=mm}">mm</RadioButton>
<RadioButton IsChecked="{Binding Path=UnitProperty, Converter={StaticResource enumBooleanConverter}, ConverterParameter=inches}">inches</RadioButton>
</StackPanel>
</ribbon:RibbonGroup>...and性质
public Unit UnitProperty
{
get
{
return Properties.Settings.Default.Units;
}
set
{
if (Properties.Settings.Default.Units != value)
{
Properties.Settings.Default.Units = value;
NotifyPropertyChanged("UnitProperty");
NotifyPropertyChanged(String.Empty);
}
}
}我必须添加NotifyPropertyChange(String.Empty);让它更新我的其他属性。
我还发现,我需要将INotifyPropertyChanged作为基类添加到通知类中。
现在我必须通过和添加处理,当用户切换到毫米和英寸之间。
发布于 2012-10-01 22:18:57
我看你需要绑定到一个枚举
我想你还需要通知。
您必须实现INotifyPropertyChanged
INotifyPropertyChanged
NotifyPropertyChanged();会通知所有
https://stackoverflow.com/questions/12681790
复制相似问题