MultiBinding Converter不显示结果

时间:2018-08-06 16:24:06

标签: c# wpf data-binding converter multibinding

作为学习在WPF中正确使用绑定的一部分,我创建了一个小型转换程序,将温度,长度,分数转换为小数等。在完成此操作后,温度和分数一切正常使用简单的绑定和转换器。我在长度转换方面遇到了问题,因为它需要MultiBinding才能起作用。

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

使用断点,我确定条目将进入Convert方法,并且该方法中的代码可以正确计算所有内容。 result变量甚至在方法调用结束时显示正确转换的数字,但是该数字并不能将其返回到lengthResult TextBox中。

我已经尝试了所有可以想到的迭代,以使其运行起来,并在Google上搜索了大约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();
    }

1 个答案:

答案 0 :(得分:1)

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

return result.ToString();

应该做到这一点。