WPF将Horizo​​ntalAlignment绑定到TextAlignment

时间:2014-09-20 02:14:31

标签: wpf

根据ListBoxItem的宽度,我有一个HorizontalAlignment Window属性由转换器更改。我在TextBlock中有一个ListBoxItem,其TextAlignment属性绑定到HorizontalAlignment的{​​{1}}属性。

ListBoxItem的{​​{1}}属性正在发生变化;但是,HorizontalAlignment的文本对齐方式保持不变。错误在哪里?

以下是绑定的代码(我没有显示ListBoxItem的转换器,因为它正常工作(根据TextBlock,它从ListBoxItem更改到Window width)):

Left

伙计,你是个侦探!我做了一个有效的实验,但我无法解释原因。我使用MultiBinding而不是Binding,传递AcualWidth属性,转换器实际上没有使用它。现在TextAlignment正在工作,并在ListBoxItem的Horizo​​ntalAlignment更改时更新。我使用了以下代码:

Center

你能解释为什么它适用于MultiBind而不是简单的Binding吗?无论如何,非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

它们是两种不同的枚举:

TextAlignment Enumeration: http://msdn.microsoft.com/en-us/library/system.windows.textalignment(v=vs.110).aspx

Horizo​​ntalAlignment枚举: http://msdn.microsoft.com/en-us/library/system.windows.horizontalalignment(v=vs.110).aspx

虽然TextAlignment包含CenterJustifyLeftRight,但HorizontalAlignment包含Center,{{1} },LeftRight。所以,正如你所看到的,它们并不相同。

如果您想基于另一个使用转换器,请创建一个转换器。

这是一个非常基本的转换器(根据您的喜好调整):

Stretch

public class HorizontalToTextAlignmentConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { TextAlignment textAlignment; // All I'm doing here is simply getting the integer value of the Enumeration. switch ((int)value) { case 0: // Left to Left textAlignment = TextAlignment.Left; break; case 1: // Center to Center textAlignment = TextAlignment.Center; break; case 2: // Right to Right textAlignment = TextAlignment.Right; break; default: // Stretch to Justify textAlignment = TextAlignment.Justify; break; } return textAlignment; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { HorizontalAlignment horizontalAlignment; // All I'm doing here is simply getting the integer value of the Enumeration. switch ((int)value) { case 0: // Left to Left horizontalAlignment = HorizontalAlignment.Left; break; case 1: // Right to Right horizontalAlignment = HorizontalAlignment.Right; break; case 2: // Center to Center horizontalAlignment = HorizontalAlignment.Center; break; default: // Justify to Stretch horizontalAlignment = HorizontalAlignment.Stretch; break; } return horizontalAlignment; } } 测试代码:

XAML

<Window.Resources> <local:HorizontalToTextAlignmentConverter x:Key="h2tAlignmentConverter"/> </Window.Resources> <Grid> <ListBox> <ListBoxItem HorizontalAlignment="Right"> <TextBlock TextWrapping="Wrap" Text="Some text" TextAlignment="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=HorizontalAlignment, Converter={StaticResource h2tAlignmentConverter}}" Width="400"/> </ListBoxItem> </ListBox> </Grid> 这些数字的顺序有点偏,所以我只是进入代码来弄清楚它们。这是他们的代码供您参考:

MSDN

修改

我对你的上一条评论有点想法,我认为由于public enum TextAlignment { Left = 0, Right = 1, Center = 2, Justify = 3, } public enum HorizontalAlignment { Left = 0, Center = 1, Right = 2, Stretch = 3, } 本身未在TextBlock父级内部对齐,您可能无法看到文本对齐方式的更改。因此,作为可能的情况之一,如果ListBoxItem大于ListBoxItem,您将看不到正确的更改。您可以通过将TextBlock的{​​{1}}绑定到自己的HorizontalContentAlignment来完成对齐的同步。

这是ListBoxItem

HorizontalAlignment

这是不同路线的直观表示。 XAML<Grid> <ListBox> <ListBoxItem HorizontalAlignment="Left" BorderThickness="1" BorderBrush="Red" Width="400" HorizontalContentAlignment="{Binding RelativeSource={RelativeSource Self}, Path=HorizontalAlignment}"> <TextBlock TextWrapping="Wrap" Text="Some text" Background="Green" Foreground="White" TextAlignment="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=HorizontalAlignment, Converter={StaticResource h2tAlignmentConverter}}" Width="300"/> </ListBoxItem> </ListBox> </Grid> ListBoxItemRedTextBlock占据整个Green&amp; ListBox

Grid路线:

enter image description here

Window路线:

enter image description here

Left路线:

enter image description here

Right对齐(文字为Center):

enter image description here

最好的部分是Stretch只是Justified,因此无需转换。