简体   繁体   English

右对齐垂直定向StackPanel的内容

[英]Right Align contents of a Vertically Oriented StackPanel

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) 我想右对齐在垂直方向的StackPanel中显示的内容,我在StackPanel本身上尝试了HorizontalAlignment="Right" ,在ListBox's DataTemplete尝试了ListBox's (在这种情况下是TextBox ,但实际上我有一个真实应用中的UserControl

here is the current result... i want the right edge of each TextBox to be right-aligned... 这是当前结果...我希望每个TextBox的右边缘右对齐...

没有右对齐

Here is the xaml, pretty simple... 这是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>

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]);
            }
        }
    }
}

Use the ListBox.ItemContainerStyle to set the HorizontalContentAlignment of the ListBoxItems to Right . 使用ListBox.ItemContainerStyleListBoxItemsHorizontalContentAlignment设置为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. 注意:这与设置HorizontalAlignment不同之处在于,项目仍将跨越对项目选择有影响的整个ListBox

Setting HorizontalAlignment on the stackpanel works for me. 在stackpanel上设置Horizo​​ntalAlignment对我有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM