Lookin at your code, in my opinion, using the Converter is not the right approach when the ComboBox selection changes. It is ok when the Width changes, but for the unit of measure I would do this:
Change the converter in this way, this is to have a common code usable by the converter and by code behind:
public class MeasureUnitsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return Convert(value);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
//From TextBox to UIElement (From string to double)
/* Under construction
if (value == null)
return 0;
else if (mu == "epx")
return value;
else if (mu == "inch")
return (double)value * 96;
else if (mu == "mm")
return (double)value * 96 * 24.5;
else
*/
return value;
}
public static string Convert(object value)
{
//From UIElement to TextBox (from double to string)
if (value == null)
return "0.00";
else if (mu == "epx")
return value.ToString(); //epx
else if (mu == "inch")
return ((double)value / 96).ToString(); //epx
else if (mu == "mm")
return ((double)value / 96 * 25.4).ToString(); //epx
else
return value.ToString();
}
}
then change cmb_SelectionChanged in this way
private void cmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is ComboBox cmb)
{
if (rect == null) return;
mu = cmb.SelectedValue.ToString();
tbx.Text = MeasureUnitsConverter.Convert(rect.Width);
}
}