2012-01-05 109 views
1

我想右對齊中顯示的垂直取向StackPanel內容,我試過HorizontalAlignment="Right"StackPanel本身和ListBox'sDataTemplete控制(在這種情況下TextBox ,但在現實中我有一個UserControl在真正的應用程序)垂直方向的StackPanel中的右對齊內容

這裏是目前的結果......我希望每個TextBox的右邊緣到右對齊...

not right-aligned

這裏是XAML,很簡單...

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:WpfApplication2="clr-namespace:WpfApplication2" 
     mc:Ignorable="d" 
     d:DataContext="{d:DesignInstance WpfApplication2:Model, IsDesignTimeCreatable=True}"> 
    <ListBox ItemsSource="{Binding Numbers}"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel /> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <TextBox Text="{Binding Mode=OneWay}" BorderBrush="Black" /> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Window> 

,這裏是我使用的是測試此使用ListBox.ItemContainerStyle設置HorizontalContentAlignment模型...

using System; 
using System.Collections.Generic; 
using System.Linq; 

namespace WpfApplication2 
{ 
    public class Model 
    { 
     [ThreadStatic] 
     static IDictionary<int, string> dict; 

     public string[] Numbers 
     { 
      get { return dict.OrderBy(x => x.Key).Select(x => x.Value).ToArray(); } 
     } 

     public Model() 
     { 
      if (dict != null) return; 
      dict = new Dictionary<int, string> 
         { 
          {1, "one"}, 
          {2, "two"}, 
          {3, "three"}, 
          {4, "four"}, 
          {5, "five"}, 
          {6, "six"}, 
          {7, "seven"}, 
          {8, "eight"}, 
          {9, "nine"}, 
          {10, "ten"}, 
          {11, "eleven"}, 
          {12, "twelve"}, 
          {13, "thirteen"}, 
          {14, "fourteen"}, 
          {15, "fifteen"}, 
          {16, "sixteen"}, 
          {17, "seventeen"}, 
          {18, "eighteen"}, 
          {19, "nineteen"}, 
          {20, "twenty"}, 
          {30, "thirty"}, 
          {40, "forty"}, 
          {50, "fifty"}, 
          {60, "sixty"}, 
          {70, "seventy"}, 
          {80, "eighty"}, 
          {90, "ninety"}, 
          {100, "one hundred"}, 
          {200, "two hundred"}, 
          {300, "three hundred"}, 
          {400, "four hundred"}, 
          {500, "five hundred"}, 
          {600, "six hundred"}, 
          {700, "seven hundred"}, 
          {800, "eight hundred"}, 
          {900, "nine hundred"}, 
          {1000, "one thousand"}, 
         }; 

      for (var number = 1; number <= 1000; number++) 
      { 
       if (dict.ContainsKey(number)) continue; 

       var divisor = number < 100 ? 10 : 100; 
       var separator = divisor == 100 ? " and " : "-"; 

       var key = (number/divisor) * divisor; 
       var mod = number % divisor; 
       dict.Add(number, dict[key] + separator + dict[mod]); 
      } 
     } 
    } 
} 
+0

不建議,物品的選擇將會變淡,因爲物品不再跨越整個寬度(同樣,如果您不需要選擇首先應該使用ItemsControl)。 – 2012-01-05 20:28:02

+0

啊好吧,我看到你的更新答案,這更有意義。謝謝!順便說一句,你一直在幫助我很多我的WPF問題,謝謝你! – 2012-01-05 20:40:08

+0

我有嗎?很高興能有幫助:) – 2012-01-05 20:40:54

回答

3

ListBoxItemsRight

<ListBox.ItemContainerStyle> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="HorizontalContentAlignment" Value="Right"/> 
    </Style> 
</ListBox.ItemContainerStyle> 

注:這是設置在該HorizontalAlignment不同的項目仍然會跨越整個ListBox這對項目選擇的影響。

0

在stackpanel上設置Horizo​​ntalAlignment對我很有用。

<ItemsPanelTemplate> 
    <StackPanel HorizontalAlignment="Right"/> 
</ItemsPanelTemplate> 
+0

項目本身仍將在面板內左對齊。 – 2012-01-05 20:31:26

+0

H.B.是正確的,這是我原本想做的事情,但它保持文本框的左對齊,同時正確對齊堆棧面板(基本上只有最寬的文本框是右對齊的,其他所有文本框都是左對齊的) – 2012-01-05 20:41:37