1

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

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

4

2 回答 2

2

查看这篇文章Silverlight 数据绑定和值转换器,他在其中解释了如何在 Silverlight 中进行数据绑定。在示例中,他使用带参数值的 ValueConverter。

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

于 2012-04-12T12:57:32.137 回答
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*/
}
于 2012-04-12T14:19:51.927 回答