用于单选/复选框的 Xaml 绑定以在英制/公制之间切换
正如标题所述,我正在从 Db 中获取一些值,这些值均以公里为单位,但我想实现一个转换器,可以在英里或公里之间切换,并希望将其绑定到显示的复选框,或单选按钮组,以最简单的为准(首选单选按钮)。
我想我可以只使用 IValueConverter
而不是 IMultiValueConverter
和 Convert/ConvertBack 方法,因为默认值为公里,但我不知道如何调用 ConvertBack 方法。或者我可以将 true/false 作为 ConverterParameter 传递,具体取决于我是否希望显示公里/英里。
但无论哪种方式,我都不确定如何在任一方法上连接 Xaml 绑定(我知道如何进行标准值转换器绑定,但不知道所需的额外内容。
任何提示表示赞赏。
<StackPanel Grid.Row="0" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Right">
<RadioButton Content="Km" GroupName="rdBtnGrpValue" IsChecked="True" />
<RadioButton Content="Miles" GroupName="rdBtnGrpValue" />
</StackPanel>
并且:
<TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Column="1" Text="{Binding EquatorialCircumference, Converter={StaticResource KmMiConv}, StringFormat='{}{0:0,0.0}'}" />
Pretty much as the title states, I'm grabbing some values from a Db, which are all in Km, but I want to implement a converter which I can toggle between Miles or Kilometers, and want to bind which is displayed to either a checkbox, or a radio button group, whichever is easiest (Radio would be preferred).
I'm thinking I can just use an IValueConverter
rather than an IMultiValueConverter
, and the Convert/ConvertBack methods, as the default will be Kilometers, but I don't know how to call the ConvertBack method. Or I could pass true/false as the ConverterParameter depending on whether I want Km/Miles displayed.
But either way I'm not sure how to hook up the Xaml Binding on either method (I know how to do a standard value converter binding, but not the extra flumff needed.
Any hints appreciated.
<StackPanel Grid.Row="0" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Right">
<RadioButton Content="Km" GroupName="rdBtnGrpValue" IsChecked="True" />
<RadioButton Content="Miles" GroupName="rdBtnGrpValue" />
</StackPanel>
And:
<TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Column="1" Text="{Binding EquatorialCircumference, Converter={StaticResource KmMiConv}, StringFormat='{}{0:0,0.0}'}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 MVVM 模式,并且使用视图模型作为
DataContext
,则可以在RadioButton
之间使用Mode=TwoWay
绑定>s 和视图模型中的布尔属性,例如 bool ConvertToImperial { get;放; }。您的实际转换可以在
EquatorialCircumference
属性的 getter 中进行。如果ConvertToImperial
为true
,则返回以英里为单位的值,否则返回以公里为单位的值。然后,对于
TextBox
,您只需将其绑定到EquatorialCircumference
属性,显示的值将采用所选单位。但是,您需要针对其值受单位更改影响的任何属性发出属性更改通知。
If you are using the MVVM pattern, and are using a view-model as your
DataContext
, you could use aMode=TwoWay
binding between theRadioButton
s and a boolean property in the view-model, something likebool ConvertToImperial { get; set; }
.Your actual conversion can occur in the getter for the
EquatorialCircumference
property. IfConvertToImperial
istrue
, return the value in miles, otherwise return the value in kilometres.Then, for the
TextBox
, you can simply bind it to theEquatorialCircumference
property, and the value displayed will be in the selected unit.You will, however, need to raise a property change notification for any properties whose values are affected by a change in units.