WPF:ListBox 项目从下到上显示

WPF: ListBox item show from bottom to top

我觉得标题不清楚 我正在使用 WPF 并创建 custom Messages control。我有 message User Control 并且消息用户控件显示在 custom Messages control[ListBox] 中。 XAML代码:

 <ListBox x:Name="uiMessages" ItemsSource="{Binding Path=Messages}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" IsItemsHost="True" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <wpfMessages:MessageDisplay>

                    </wpfMessages:MessageDisplay>
                </DataTemplate>
            </ListBox.ItemTemplate>
</ListBox>

当前视图:

预期:

现在列表框项目从上到下显示,红色消息后是空的space。如果 2 条绿色消息在底部,红色消息在顶部,我会很好。预期是从 botton 到 top 添加消息,而不是像现在这样从 top 到 botton。

有什么想法吗?

提前致谢 Jamaxack!

有点不清楚,但我认为您想更改 ItemsPanel 布局。

<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <VirtualizingStackPanel VerticalAlignment="Bottom"/>
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>

创建了反转元素的自定义 WrapPanel

XAML代码:

 <ListBox x:Name="uiMessages" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
         <ListBox.ItemsPanel>
              <ItemsPanelTemplate>
                   <wpf:ReverseWrapPanel Orientation="Horizontal"/>
              </ItemsPanelTemplate>
         </ListBox.ItemsPanel>
   </ListBox>

后面的代码:

  public class ReverseWrapPanel : WrapPanel
    {
        public new Orientation Orientation { get { return base.Orientation; } set { base.Orientation = value; ResetAll(); } }

        /// <summary>
        /// The opposite of the Orientation property.
        /// </summary>
        Orientation FlipDirection { get { return Orientation == Orientation.Horizontal ? Orientation.Vertical : Orientation.Horizontal; } }

        public ReverseWrapPanel()
        {
            Initialized += ReverseWrapPanel_Initialized;
        }

        void ReverseWrapPanel_Initialized(object sender, System.EventArgs e)
        {
            this.Mirror(FlipDirection);
        }

        protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
        {
            base.OnVisualChildrenChanged(visualAdded, visualRemoved);

            foreach (UIElement child in Children.OfType<UIElement>())
            {
                child.Mirror(FlipDirection);
            }
        }

        void ResetAll()
        {
            this.Mirror(FlipDirection);

            foreach (UIElement child in Children.OfType<UIElement>())
            {
                child.Mirror(FlipDirection);
            }
        }
    }

感谢 Jamshed!