在WPF中,如何绑定到属性的一小部分?

时间:2011-07-14 07:17:19

标签: .net wpf xaml data-binding

如何将某些东西绑定到Path值的一小部分? Path = ActualPath / 2似乎不起作用。

Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, 
                   Path=ActualHeight / 2}">

2 个答案:

答案 0 :(得分:3)

您可以使用ValueConverter执行此操作,例如:

class MakeHalfConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((double)value)/2;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((double)value)*2;
    }
}

答案 1 :(得分:1)

没有开箱即用。您需要使用值转换器。
查看几个即用型转换器here。您可以使用ExpressionConverter作为您的方案。