Binding to element in WPF: can the Path expression do math?

后端 未结 5 1102
天命终不由人
天命终不由人 2021-01-31 09:34

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

相关标签:
5条回答
  • 2021-01-31 09:34

    I use a MathConverter to do math in my XAML bindings.The converter code can be found here and it is used like this:

    Height="{Binding ElementName=RootWindow, Path=ActualHeight,
                     Converter={StaticResource MathConverter},
                     ConverterParameter=@VALUE/2}"
    

    It will also handle more advanced math equations like

    Height="{Binding ElementName=RootWindow, Path=ActualHeight,
                    Converter={StaticResource MathConverter},
                    ConverterParameter=((@VALUE-200)*.3)}"
    
    0 讨论(0)
  • 2021-01-31 09:34

    No, standart binding doesn't support expressions in Path. But you can look at my project CalcBinding, which was developed specially to resolve this problem and some others. Say, you can write something like:

    <Button Content="{c:Binding ElementName=grid, Path=ActualWidth+Height}"/>
    

    or

    <Label Content="{c:Binding A+B+C }" />
    

    or

    <Button Visibility="{c:Binding IsChecked, FalseToVisibility=Hidden}" />
    

    where A, B, C, IsChecked - properties of viewModel and it will work properly

    Goodluck!

    0 讨论(0)
  • 2021-01-31 09:41

    Take a look at my MathConverter project. It allows for very advanced expressions, including string formatting.

    In particular, your expression would be handled as such:

    Height="{Binding ActualHeight, ConverterParameter=x/2, Converter={StaticResource math}}"
    

    There are loads of examples, as well as a basic introduction of how to use it on the project's homepage.

    0 讨论(0)
  • 2021-01-31 09:42

    @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();
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-31 10:01

    No it can't you should use binding converters

    public class MyConverter : IValueConverter
    {
    public object Convert(object value, Type  targetType,
          object parameter, CultureInfo culture)
      {
          return (int)value/2;
      }
    
      public object ConvertBack(object value, Type targetType,
          object parameter, CultureInfo culture)
      {
        return null;
      }
    }
    
    0 讨论(0)
提交回复
热议问题