gpt4 book ai didi

c# - 使 ListBox 大小适合用作 ItemsPresenter 的 ItemsPanel

转载 作者:太空宇宙 更新时间:2023-11-03 11:08:51 25 4
gpt4 key购买 nike

我有一个自定义控件 ListBox,我想根据其 ItemsPanel 的大小调整其大小。对于项目面板,我有一个自定义控件 WrapPanel 可以相应地排列项目。正如您在屏幕截图中所见,ListBox 更喜欢根据其父控件或 availableSize 调整自身大小。

然后我尝试创建一个自定义控件 Grid,它有一个 ItemsSource 属性,可以将项目传递到它的列表框。但这也没有用。当 Grid 会排列时,ListBox 会排列,这会导致 Grid 排列等等。

所以我的问题是,如何创建一个自定义控件,该控件具有 ItemsSource 属性和一个根据其子项的内容调整自身大小的 ItemsPresenter?

Screenshot of ListBox Custom Control

最佳答案

您只需将 ListBox Horizo​​ntalAlignment 设置为 Left,并将 VerticalAlignment 设置为 Top。这应该可以解决问题。

简单的例子:

主窗口.xaml

<Window x:Class="ListBoxFitItemsPanel.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="400">
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Red" HorizontalAlignment="Left" VerticalAlignment="Top" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Background="AntiqueWhite" Margin="5" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>

<Rectangle Width="100" Height="100" Fill="LightSlateGray" Stroke="Black" StrokeThickness="1" Margin="5" />
<Rectangle Width="100" Height="100" Fill="LightSlateGray" Stroke="Black" StrokeThickness="1" Margin="5" />
<Rectangle Width="100" Height="100" Fill="LightSlateGray" Stroke="Black" StrokeThickness="1" Margin="5" />
<Rectangle Width="100" Height="100" Fill="LightSlateGray" Stroke="Black" StrokeThickness="1" Margin="5" />
<Rectangle Width="100" Height="100" Fill="LightSlateGray" Stroke="Black" StrokeThickness="1" Margin="5" />
<Rectangle Width="100" Height="100" Fill="LightSlateGray" Stroke="Black" StrokeThickness="1" Margin="5" />
<Rectangle Width="100" Height="100" Fill="LightSlateGray" Stroke="Black" StrokeThickness="1" Margin="5" />
<Rectangle Width="100" Height="100" Fill="LightSlateGray" Stroke="Black" StrokeThickness="1" Margin="5" />
<Rectangle Width="100" Height="100" Fill="LightSlateGray" Stroke="Black" StrokeThickness="1" Margin="5" />
</ListBox>
</Window>

enter image description here

编辑:它也适用于绑定(bind)场景:

MainWindow.xaml

<Window x:Class="ListBoxFitItemsPanel.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ListBoxFitItemsPanel"
Title="MainWindow" Height="400" Width="400">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Item}">
<Rectangle Width="100" Height="100" Fill="LightSlateGray" Stroke="Black" StrokeThickness="1" Margin="5" />
</DataTemplate>
</Window.Resources>
<ListBox ItemsSource="{Binding Items}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Red" HorizontalAlignment="Left" VerticalAlignment="Top" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Background="AntiqueWhite" Margin="5" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Window>

MainWindow.xaml.cs

using System.Collections.Generic;
using System.Windows;

namespace ListBoxFitItemsPanel
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}

public IEnumerable<Item> Items
{
get
{
for (int i = 0; i < 9; i++)
{
yield return new Item();
}
}
}
}

public class Item { }
}

关于c# - 使 ListBox 大小适合用作 ItemsPresenter 的 ItemsPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14775480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com