I\'m trying to bind a control to the parent\'s Height/width property using ElementName
and a Path. However, I don\'t want to bind to the actual height, but to exact
@Rachel's MathConverter worked great for me, however I switched out the expression parsing and just left that bit to NCalc. That way I didn't have to worry about operator precedence.
using NCalc;
using System;
using System.Globalization;
using System.Windows.Data;
namespace MyProject.Utilities.Converters
{
public class MathConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Parse value into equation and remove spaces
string expressionString = parameter as string;
expressionString = expressionString.Replace(" ", "");
expressionString = expressionString.Replace("@VALUE", value.ToString());
return new Expression(expressionString).Evaluate();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}