2012-01-05 2 views
1

나는 수직으로 StackPanel 방향에 표시되는 내용을 마우스 오른쪽 정렬 할,이 경우 TextBox에합니다 (StackPanel 자체와 ListBox'sDataTemplete의 제어에 HorizontalAlignment="Right"을 시도했습니다 하지만, 현실에서 내가 ... 내가 각 TextBox의 오른쪽 가장자리를 잘 정렬되고 싶어 ... 여기 오른쪽 정렬 내용이

현재 결과) 실제 응용 프로그램에서 UserControl

not right-aligned

여기

은 ...

<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> 

매우 간단 XAML이고, 여기에 내가 이것을 테스트하기 위해 사용하고 모델 ...

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

것을 권장하지 않을까요 항목의 선택은 dimished됩니다. –

+0

아아아, 나는 당신의 업데이트 된 답변을 보았다. 감사! btw 당신은 내 wpf 질문을 많이 도와 줘서 고마워, 그 덕분에! –

+0

나? 도움이되기를 기원합니다. –

답변

3

HorizontalContentAlignment을 설정 ListBox.ItemContainerStyle를 사용입니다 ListBoxItems ~ Right

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

참고 :이 항목은 아직 항목 선택에 영향을 전체 ListBox에 걸쳐 것의 HorizontalAlignment을 설정 다르다.

0

스택 패널의 HorizontalAlignment 설정이 유용합니다. (당신이 ItemsControl에 사용해야 처음에 선택을하지 않아도 또한 경우) 항목이 더 이상 전체 폭에 걸쳐되지 않는

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

항목 자체는 여전히 패널 내에서 왼쪽 정렬됩니다. –

+0

H.B. 맞습니다, 이것은 원래 내가하려고했던 것이 었 습니다만, 스택 패널을 오른쪽 정렬하는 동안 텍스트 상자의 왼쪽 정렬을 유지합니다 (기본적으로 가장 넓은 텍스트 상자 만 오른쪽 정렬되고 다른 모든 텍스트 상자는이 텍스트 상자에 왼쪽 정렬 됨) –