作为学习在WPF中正确使用绑定的一部分,我创建了一个小的转换程序,将温度、长度、分数转换为小数等。温度和分数的一切都很好,因为这些都是通过简单的绑定和转换器完成的。不过,我在长度转换方面遇到了一个问题,因为它需要一个MultiBinding才能发挥作用。
对于控件,我使用两个文本框表示原始长度和转换后的长度。每个都有一个匹配的组合框,用户可以在其中选择测量单位(即厘米、英寸、毫米等(。
使用断点,我已经确定条目正在进入Convert方法,并且该方法中的代码正在正确地计算所有内容。result
变量甚至在方法调用结束时显示正确转换的数字,但该数字不会返回到lengthResult
TextBox中。
我已经尝试了我能想到的每一次迭代,并在谷歌上搜索了大约3个小时。我找不到这个问题,也找不到一本能显示类似情况的指南,我无法使其适用于这种情况。有人能不能仔细看看,发现我犯的(很可能(这个简单的错误?
注意:我在C#代码中有一个ConvertBack
函数,但其中除了默认的throw new NotImplementedException
之外什么都没有,所以我省略了它。在Convert
首先工作后,我会担心这个问题。:(
XAML:
<GroupBox Header="Length" DockPanel.Dock="Top">
<GroupBox.Resources>
<local:LengthConverter x:Key="lengthConverter" />
</GroupBox.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Content="Original Length:" Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="10" />
<Label Content="Destination Length:" Grid.Column="0" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="10" />
<ComboBox x:Name="originUnitSelect" Grid.Column="2" Grid.Row="0" ItemsSource="{Binding Units}" Height="{Binding ElementName=lengthEntry, Path=ActualHeight}" MinWidth="50" />
<ComboBox x:Name="destinationUnitSelect" Grid.Column="2" Grid.Row="1" ItemsSource="{Binding Units}" Height="{Binding ElementName=lengthResult, Path=ActualHeight}" MinWidth="50" />
<TextBox x:Name="lengthEntry" Grid.Column="1" Grid.Row="0" VerticalAlignment="Center" Margin="10" HorizontalContentAlignment="Center" MinWidth="250" />
<TextBox x:Name="lengthResult" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="10" HorizontalContentAlignment="Center" MinWidth="250">
<TextBox.Text>
<MultiBinding Converter="{StaticResource lengthConverter}">
<Binding ElementName="lengthEntry" Path="Text" UpdateSourceTrigger="PropertyChanged" />
<Binding ElementName="originUnitSelect" Path="SelectedValue" />
<Binding ElementName="destinationUnitSelect" Path="SelectedValue" />
</MultiBinding>
</TextBox.Text>
</TextBox>
<Button x:Name="convertLength" Grid.Column="1" Grid.Row="2" Content="Convert" VerticalAlignment="Center" HorizontalAlignment="Center" Click="convertLength_Click" Margin="10"/>
</Grid>
</GroupBox>
C#:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
double result = 0.0;
double cm_to_mm = 10;
double cm_to_in = 0.3937;
double cm_to_ft = 0.32808;
double cm_to_yd = 0.010936;
double mm_to_cm = 0.10;
double mm_to_in = mm_to_cm * cm_to_in;
double mm_to_ft = mm_to_cm * cm_to_ft;
double mm_to_yd = mm_to_cm * cm_to_yd;
double in_to_mm = 25.4;
double in_to_cm = 2.54;
double in_to_ft = 1 / 12;
double in_to_yd = 1 / 36;
double ft_to_yd = 36;
double ft_to_in = 12;
double ft_to_cm = ft_to_in * in_to_cm;
double ft_to_mm = ft_to_in * in_to_mm;
double yd_to_ft = 3;
double yd_to_in = 36;
double yd_to_cm = ft_to_in * in_to_cm;
double yd_to_mm = ft_to_in * in_to_mm;
if(values[1] != null && values[2] != null)
{
switch(values[1].ToString())
{
case "mm":
if (double.TryParse(values[0].ToString(), out result))
{
switch(values[2].ToString())
{
case "cm":
result = result * mm_to_cm;
break;
case "in":
result = result * mm_to_in;
break;
case "ft":
result = result * mm_to_ft;
break;
case "yd":
result = result * mm_to_yd;
break;
}
return result;
}
break;
case "cm":
if(double.TryParse(values[0].ToString(), out result))
{
switch(values[2].ToString())
{
case "mm":
result = result * cm_to_mm;
break;
case "in":
result = result * cm_to_in;
break;
case "ft":
result = result * cm_to_ft;
break;
case "yd":
result = result * cm_to_yd;
break;
}
return result;
}
break;
case "in":
if(double.TryParse(values[0].ToString(), out result))
{
switch(values[2].ToString())
{
case "mm":
result = result * in_to_mm;
break;
case "cm":
result = result * in_to_cm;
break;
case "ft":
result = result * in_to_ft;
break;
case "yd":
result = result * in_to_yd;
break;
}
return result;
}
break;
case "ft":
if(double.TryParse(values[0].ToString(), out result))
{
switch(values[2].ToString())
{
case "mm":
result = result * ft_to_mm;
break;
case "cm":
result = result * ft_to_cm;
break;
case "in":
result = result * ft_to_in;
break;
case "yd":
result = result * ft_to_yd;
break;
}
return result;
}
break;
case "yd":
if(double.TryParse(values[0].ToString(), out result))
{
switch(values[2].ToString())
{
case "mm":
result = result * yd_to_mm;
break;
case "cm":
result = result * yd_to_cm;
break;
case "in":
result = result * yd_to_in;
break;
case "ft":
result = result * yd_to_ft;
break;
}
}
break;
}
}
return result;
//throw new NotImplementedException();
}
textbox
需要一个字符串,而不是double
。
return result.ToString();
应该做到。