简体   繁体   中英

MultiBinding Converter Not Displaying Results

As part of learning to use binding in WPF correctly, I've created a small conversion program, converting things like temperatures, length, fractions to decimals, etc. Everything has worked fine with the temperatures and fractions, as these are done with simple bindings and converters. I'm running into a problem though with the length conversion, as it requires a MultiBinding in order to function.

For controls, I'm using two textboxes for the original length and converted length. Each of these has a matching ComboBox where the user can select the unit of measurement (ie-cm, in, mm, etc).

Using breakpoints, I've established that the entries are getting to the Convert method, and that the code inside the method is calculating everything correctly. The result variable even shows the correctly converted number at the end of the method call, but that number isn't making it back into the lengthResult TextBox.

I've tried every iteration I could think of to get this to work, and googled this for about 3 hours now. I can't find the problem or a guide that shows a similar enough situation that I can make it work for this situation. Can somebody please look this over and find the (very likely) simple mistake I'm making?

Note: I have a ConvertBack function in the C# code, but there's nothing in it yet beyond the default throw new NotImplementedException , so I left it out. I'll worry about that after the Convert works first. :)

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();
    }

The textbox needs a string, not a double .

return result.ToString();

Should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM