我想右对齐垂直方向StackPanel
中显示的内容,我在HorizontalAlignment="Right"
本身和StackPanel
中的控件上尝试ListBox's
} DataTemplete
(在这种情况下为TextBox
,但实际上我在真实应用中有UserControl
这是当前的结果......我希望每个TextBox
的右边缘都是右对齐...
这是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>
这是我用来测试它的模型......
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 :(得分:3)
使用ListBox.ItemContainerStyle
将ListBoxItems
的{{3}}设置为Right
。
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Right"/>
</Style>
</ListBox.ItemContainerStyle>
注意:这与设置HorizontalAlignment
的不同之处在于,项目仍会覆盖整个ListBox
,这会对项目选择产生影响。
答案 1 :(得分:0)
在stackpanel上设置HorizontalAlignment对我有用。
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Right"/>
</ItemsPanelTemplate>