這是兩個不同的枚舉:
TextAlignment枚舉:http://msdn.microsoft.com/en-us/library/system.windows.textalignment(v=vs.110).aspx
的HorizontalAlignment枚舉:http://msdn.microsoft.com/en-us/library/system.windows.horizontalalignment(v=vs.110).aspx
雖然TextAlignment
包含Center
,Justify
,Left
和Right
,HorizontalAlignment
包含Center
,Left
,Right
和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
這些numerations的順序是有點過了,所以我就走進了代碼來找出它們。這裏是他們的,供大家參考代碼:
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
,則不會看到適當的更改。您可以通過將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>
下面是不同的對準的視覺表示。 ListBoxItem
是Red
,TextBlock
是Green
和ListBox
佔據整個Grid
& Window
。
Left
對準:
Right
對準:
Center
對準:
Stretch
對準(文本是Justified
):
的最好的部分是HorizontalContentAlignment
是一個簡單的HorizontalAlignment
,因此不需要轉換。
首先'TextAlignment'&'HorizontalAlignment'是兩個不同的枚舉,因此可能不完全兼容。但你可以在綁定中添加'Mode = TwoWay'來嘗試一下。 – pushpraj 2014-09-20 02:20:56
感謝您的快速回答!我試過這個,但沒有奏效。有沒有辦法將HorizontalAligment轉換爲TextAlignment? – user3952846 2014-09-20 02:26:24