Binding Converter如何绑定到控件的属性可以访问控件的其他属性

时间:2012-04-12 12:40:10

标签: wpf silverlight windows-phone-7 binding ivalueconverter

我正在尝试将值“MaxLines”绑定到WP7应用中的TextBlock的Height属性。有一个绑定转换器,它应该使LineHeight与MaxLines复用并返回预期的高度。我想说的是我想控制TextBlock中显示的行数。我如何能够从转换器访问TextBlock的LineHeight属性。

为了使这个通用我不想单独维护LineHeights或从viewModel访问它们

2 个答案:

答案 0 :(得分:2)

查看这篇文章Silverlight data binding and value converters,他解释了如何在Silverlight中使用Databind。在示例中,他使用带参数值的ValueConverter。

我认为这就是您所需要的,只需将LineHeight绑定到参数即可。 (你可以使用Blend)

答案 1 :(得分:1)

您可以使用ConverterParameter:

<TextBlock x:Name="MyTextBlock" Height="{Binding ConverterParameter=Height, ElementName=MyTextBlock, Converter={StaticResource SomeConverter}}" Text="{Binding SomeLongText}" />

或传递整个文本块:

<TextBlock x:Name="MyTextBlock" Height="{Binding Converter={StaticResource ImageFileConverter}, ElementName=DropdownImage}" Text="{Binding SomeLongText}" />

然后在控制器内:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var image = value as TextBlock;
            /*do your magic here*/
}
相关问题