2
votes

I have a lot of controls in my XAML in the form of TextBlock: TextBox.

So for example:

XSize(mm):25.4

YSize(mm):50.8 etc

Now when the user clicks on an option to use imperial units I want to change all textBlocks + textBoxes to something like

XSize(in):1

YSize(in):2

etc

What is the best way to go about this ?

1
Without code, it is usually hard to guess what the exact problem is, and guessing for answers is usually the only thing anybody can do. If you provide a piece of code of your previous attempts, it will be a lot easier to understand what the exact problem you're having is. That piece of code can even be code that doesn't work at all, because it would still give a better idea of what you are trying to do exactly.Joeytje50

1 Answers

2
votes

Try to use Converters. Create a MM to Inch converter and use that converter to change the values when the user inputs a value on the certain value.

Sample usage of a converter. You have to define a static resource under the resources of your view for the controls you want to use the converter with

MainWindow.xaml

<Window.Resources>
    <spikes:Convertaaa x:Key="Convertaaa" />
</Window.Resources>
<ComboBox x:Name="OptionsToChoose"/>
<TextBox>
    <TextBox.Text>
        <MultiBinding Converter="{StaticResource Convertaaa}">
            <Binding ElementName="OptionsToChoose" Path="SelectedValue"/>
            <Binding RelativeSource="{RelativeSource Self}" Path="Text"/>
        </MultiBinding>
    </TextBox.Text>
</TextBox>

Converter.cs

public class Convertaaa : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        //Implement conversion here. values[0] will give you the selected option values[1] will give you the value to convert, then do a return
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

This assumes that you do know MVVM pattern in WPF and Bindings. If you are doing behind the code, then you probably want to hook up to the TextChanged of your TextBox and instantiate a new Converter Class and call the Convert method and set the Text property of the Textbox.