我现在最终得到了一些类似的东西。仍然会享受不需要 DataTrigger 为每个可能值提供的解决方案。
这与@SamTheDev 发布的答案有点不同,但大致相同。
xml
<UserControl x:Class="MyNamespace.Controls.MeasurementTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:MyNamespace.Converters"
xmlns:b="clr-namespace:MyNamespace.Behaviors"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
x:Name="root">
<UserControl.Resources>
<c:MeasurementUnitConverter x:Key="muc"/>
<c:MeasurementConverter2 x:Key="mc"/>
<sys:Boolean x:Key="BooleanFalse">False</sys:Boolean>
<sys:Boolean x:Key="BooleanTrue">True</sys:Boolean>
</UserControl.Resources>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<TextBox Margin="0" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" HorizontalContentAlignment="Right" VerticalAlignment="Stretch"
b:AutoSelectBehavior.AutoSelect="True">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding UseMetric, ElementName=root}" Value="True">
<Setter Property="Text" Value="{Binding Measurement, Mode=TwoWay, ElementName=root, Converter={StaticResource mc}, ConverterParameter={StaticResource BooleanTrue}}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding UseMetric, ElementName=root}" Value="False">
<Setter Property="Text" Value="{Binding Measurement, Mode=TwoWay, ElementName=root, Converter={StaticResource mc}, ConverterParameter={StaticResource BooleanFalse}}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<!-- in or mm label -->
<Label VerticalAlignment="Center" Padding="0" Margin="5" HorizontalAlignment="Left" Grid.Column="1"
Content="{Binding UseMetric, ElementName=root, Converter={StaticResource muc}}"/>
</Grid>
</UserControl>
xml.cs
using System;
using System.Windows;
using System.Windows.Controls;
namespace MyNamespace.Controls
{
/// <summary>
/// Interaction logic for MeasurementTextBox.xaml
/// </summary>
public partial class MeasurementTextBox : UserControl
{
public MeasurementTextBox()
{
// This call is required by the designer.
InitializeComponent();
}
public bool UseMetric {
get { return Convert.ToBoolean(GetValue(UseMetricProperty)); }
set { SetValue(UseMetricProperty, value); }
}
public static readonly DependencyProperty UseMetricProperty = DependencyProperty.Register("UseMetric", typeof(bool), typeof(MeasurementTextBox), new PropertyMetadata(MeasurementTextBox.UseMetricChanged));
private static void UseMetricChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
public double Measurement {
get { return (double)GetValue(MeasurementProperty); }
set { SetValue(MeasurementProperty, value); }
}
public static readonly DependencyProperty MeasurementProperty = DependencyProperty.Register("Measurement", typeof(double), typeof(MeasurementTextBox), new PropertyMetadata(MeasurementTextBox.MeasurementPropertyChanged));
private static void MeasurementPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
}
}
转换器
using System;
using System.Windows;
using System.Windows.Data;
namespace MyNamespace.Converters
{
class MeasurementConverter : IValueConverter
{
const double MILLIMETERS_IN_ONE_INCH = 25.4;
const string INCHES_ABBREVIATION = "in";
const string MILLIMETERS_ABBREVIATION = "mm";
const double ONE_THIRTY_SECOND = 0.03125;
const double ONE_SIXTEENTH = 0.0625;
const double ONE_EIGHTH = 0.125;
const double ONE_FOURTH = 0.25;
const double ONE_HALF = 0.5;
const double ONE = 1;
public double RoundToNearest(double value, int unitPrecision)
{
double fraction = 0;
int reciprocal = 0;
switch (unitPrecision)
{
case 0:
fraction = ONE;
reciprocal = (int)ONE;
break;
case 1:
fraction = ONE;
reciprocal = (int)ONE;
break;
case 2:
fraction = ONE_HALF;
reciprocal = (int)(1 / ONE_HALF);
break;
case 3:
fraction = ONE_FOURTH;
reciprocal = (int)(1 / ONE_FOURTH);
break;
case 4:
fraction = ONE_EIGHTH;
reciprocal = (int)(1 / ONE_EIGHTH);
break;
case 5:
fraction = ONE_SIXTEENTH;
reciprocal = (int)(1 / ONE_SIXTEENTH);
break;
case 6:
fraction = ONE_THIRTY_SECOND;
reciprocal = (int)(1 / ONE_THIRTY_SECOND);
break;
}
return Math.Round(value * reciprocal, MidpointRounding.AwayFromZero) * fraction;
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string strValue = (string)value;
bool isMetric = (bool)parameter;
double enteredValue = 0;
bool enteredValueIsImperial = false;
if (strValue.EndsWith(INCHES_ABBREVIATION))
{
enteredValueIsImperial = true;
strValue = strValue.Substring(0, strValue.Length - INCHES_ABBREVIATION.Length);
}
else if (strValue.EndsWith(MILLIMETERS_ABBREVIATION))
{
enteredValueIsImperial = false;
strValue = strValue.Substring(0, strValue.Length - MILLIMETERS_ABBREVIATION.Length);
}
else if (isMetric)
{
enteredValueIsImperial = false;
}
else
{
enteredValueIsImperial = true;
}
try
{
enteredValue = double.Parse(strValue);
}
catch (FormatException)
{
return DependencyProperty.UnsetValue;
}
if (isMetric)
{
if (enteredValueIsImperial)
{
//inches to mm
return RoundToNearest(enteredValue * MILLIMETERS_IN_ONE_INCH, 0);
//0 is mm
}
else
{
//mm to mm
return RoundToNearest(enteredValue, 0);
//0 is mm
}
}
else
{
if (enteredValueIsImperial)
{
//inches to inches
return RoundToNearest(enteredValue, 5);
}
else
{
//mm to inches
return RoundToNearest(enteredValue / MILLIMETERS_IN_ONE_INCH, 5);
}
}
}
}
}
用法:
<mynamespace:MeasurementTextBox Measurement="{Binding SomeLength, Mode=TwoWay}"
UseMetric="{Binding IsMetric}"/>