代码之家  ›  专栏  ›  技术社区  ›  Keven M

多绑定转换器不显示结果

  •  0
  • Keven M  · 技术社区  · 6 年前

    作为学习在WPF中正确使用绑定的一部分,我创建了一个小的转换程序,将诸如温度、长度、分数转换为小数等内容。所有内容都可以很好地处理温度和分数,因为这些都是通过简单的绑定和转换器完成的。我遇到了一个长度转换的问题,因为它需要一个多重绑定才能运行。

    对于控件,我使用两个文本框作为原始长度和转换长度。每个都有一个匹配的组合框,用户可以在其中选择测量单位(即厘米、英寸、毫米等)。

    通过使用断点,我已经确定了条目正在到达Convert方法,并且方法中的代码正在正确计算所有内容。这个 result 变量甚至在方法调用结束时显示正确转换的数字,但该数字没有使其返回到 lengthResult 文本框。

    我已经尝试了我能想到的每一个迭代来让这个工作,并在谷歌上搜索了大约3个小时。我找不到问题,也找不到一个能说明类似情况的指南,我无法使它适用于这种情况。有人能看看这个吗 (很可能) 我犯了个简单的错误?

    注:我有一个 ConvertBack C代码中的函数,但它中没有超出默认值的内容 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();
        }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   eye_am_groot    6 年前

    这个 textbox 需要一个字符串,而不是 double .

    return result.ToString();
    

    应该会成功的。