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

后端 未结 5 1104
天命终不由人
天命终不由人 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)}"
    

提交回复
热议问题