2014-09-20 48 views
1

我有一個ListBoxItemHorizontalAlignment屬性由轉換器根據Window的寬度更改。我在ListBoxItem內有TextBlock,其TextAlignment屬性綁定到ListBoxItemHorizontalAlignment屬性。WPF綁定水平對齊到文本對齊

HorizontalAlignmentListBoxItem的屬性正在改變;但是,TextBlock的文本對齊保持不變。錯誤在哪裏?

這裏是綁定(我沒有顯示(根據Window widthListBoxItem的,因爲它工作正常的轉換器,它改變了從LeftCenter))的代碼:

<ListBox> 
    <ListBoxItem> 
     <TextBlock TextWrapping="Wrap" Text="Some text" 
        TextAlignment="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=HorizontalAlignment}"/> 
    </ListBoxItem> 
</ListBox> 

人,你是一個偵探!我做了一個可行的實驗,但我無法解釋爲什麼。我使用MultiBinding而不是Binding,傳遞了AcualWidth屬性,這實際上並不是由Converter使用的。現在TextAlignment正在工作,並在ListBoxItem的Horizo​​ntalAlignment更改時進行更新。我用下面的代碼:

<TextBlock TextWrapping="Wrap" Text="Some text"> 
    <TextBlock.TextAlignment> 
     <MultiBinding Converter="{StaticResource HorizontalToTextAlignmentConverter}"> 
      <Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/> 
      <Binding Path="HorizontalAlignment" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}"/> 
     </MultiBinding> 
    </TextBlock.TextAlignment> 
</TextBlock> 

你能解釋爲什麼它與多路捆綁合作,而不是一個簡單的綁定?無論如何,非常感謝您的幫助!

+0

首先'TextAlignment'&'Horizo​​ntalAlignment'是兩個不同的枚舉,因此可能不完全兼容。但你可以在綁定中添加'Mode = TwoWay'來嘗試一下。 – pushpraj 2014-09-20 02:20:56

+0

感謝您的快速回答!我試過這個,但沒有奏效。有沒有辦法將Horizo​​ntalAligment轉換爲TextAlignment? – user3952846 2014-09-20 02:26:24

回答

0

這是兩個不同的枚舉:

TextAlignment枚舉: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包含CenterJustifyLeftRightHorizontalAlignment包含CenterLeftRightStretch。所以,如你所見,他們不一樣。

創建一個轉換器,如果你想使用一個基於另一個。

這裏是一個非常基本的轉換器(調整到您喜歡):

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,則不會看到適當的更改。您可以通過將HorizontalContentAlignmentListBoxItem綁定到其自己的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> 

下面是不同的對準的視覺表示。 ListBoxItemRed,TextBlockGreenListBox佔據整個Grid & Window

Left對準:

enter image description here

Right對準:

enter image description here

Center對準:

enter image description here

Stretch對準(文本是Justified):

enter image description here

的最好的部分是HorizontalContentAlignment是一個簡單的HorizontalAlignment,因此不需要轉換。

+0

你說得對。我會嘗試使用轉換器。謝謝! – user3952846 2014-09-20 02:32:34

+0

BK,非常感謝您的幫助!只有第一個開關的第一個和第二個選項相反。但TextBlock中的屬性仍未更新。它正確啓動(TextBlock TextAlignment在ListBoxItem開始Streched時開始對齊),但是,當ListBoxItem更改爲Center時,TextAlignment不會更改)。我不知道發生了什麼。 – user3952846 2014-09-20 03:36:37

+0

@ user3952846是的,我修正了代碼,請參考最新版本。 「MSDN」的訂單已關閉。我已經測試過,並且沒有任何問題(確保您使用我最新的Converter代碼)。 – 2014-09-20 04:10:21