Reputation: 114826
I want to right-align the contents that are displayed in a vertically oriented StackPanel
, I've tried HorizontalAlignment="Right"
on the StackPanel
itself and the control in the ListBox's
DataTemplete
(in this case a TextBox
, but in reality I have a UserControl
in the real app)
here is the current result... i want the right edge of each TextBox
to be right-aligned...
Here is the xaml, pretty simple...
<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>
and here is the model I'm using to test this out with...
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]);
}
}
}
}
Upvotes: 1
Views: 566
Reputation: 184306
Use the ListBox.ItemContainerStyle
to set the HorizontalContentAlignment
of the ListBoxItems
to Right
.
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Right"/>
</Style>
</ListBox.ItemContainerStyle>
Note: This is different from setting the HorizontalAlignment
in that the items will still span the whole ListBox
which has an impact on item selection.
Upvotes: 3
Reputation: 9677
Setting HorizontalAlignment on the stackpanel works for me.
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Right"/>
</ItemsPanelTemplate>
Upvotes: 0