ItemsControl 中的 WrapPanel 添加了 ScrollViewer 导致失去换行行为

发布于 2024-10-28 06:13:43 字数 1618 浏览 2 评论 0原文

我正在 ItemsControl 内的 ItemsPanel 内使用 WrapPanel。只要添加的物品符合尺寸,这种方法就完美。为了满足内容超出尺寸的情况,我添加了ScrollViewer。但现在这些项目仅垂直列出,即使它们都适合。知道是否可能以及我必须做什么。

这是我的 xaml:

<ItemsControl Name="LicSwUserItemsCtl" Grid.Column="1" Grid.Row="2">
<ItemsControl.Template>
    <ControlTemplate>
        <ScrollViewer Name="Scroller" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
            <ItemsPresenter />
        </ScrollViewer>
    </ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <toolkit:WrapPanel Orientation="Vertical"  HorizontalAlignment="Stretch" Margin="0" Name="wrpPanel" VerticalAlignment="Stretch" />
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Border x:Name="BrdrStkPnlSwLicUser" CornerRadius="2" BorderThickness="1" Width="120" Height="20" Margin="0,0,3,0" BorderBrush="Transparent">
            <StackPanel Name="StkPnlSwLicUser"  Orientation="Horizontal" Height="18" Width="120" Margin="0" MouseEnter="StackPanel_MouseEnter" MouseLeave="StackPanel_MouseLeave" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp" >
                <Image Source="{Binding Converter={StaticResource IconImage}}" Margin="2,0,3,0" Height="16" Width="16" />
                <TextBlock Name="txtBlk" Text="{Binding NodeText}" />
            </StackPanel>
        </Border>
    </DataTemplate>
</ItemsControl.ItemTemplate>

I'm working with a WrapPanel within an ItemsPanel within an ItemsControl. This works perfect as long as the items added fit the size. To meet the situations where the content exceeds the size, I added a ScrollViewer. But now the items are listed only vertically even if all they would fit. Any idea if it is possible and what I have to do.

Here is my xaml:

<ItemsControl Name="LicSwUserItemsCtl" Grid.Column="1" Grid.Row="2">
<ItemsControl.Template>
    <ControlTemplate>
        <ScrollViewer Name="Scroller" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
            <ItemsPresenter />
        </ScrollViewer>
    </ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <toolkit:WrapPanel Orientation="Vertical"  HorizontalAlignment="Stretch" Margin="0" Name="wrpPanel" VerticalAlignment="Stretch" />
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Border x:Name="BrdrStkPnlSwLicUser" CornerRadius="2" BorderThickness="1" Width="120" Height="20" Margin="0,0,3,0" BorderBrush="Transparent">
            <StackPanel Name="StkPnlSwLicUser"  Orientation="Horizontal" Height="18" Width="120" Margin="0" MouseEnter="StackPanel_MouseEnter" MouseLeave="StackPanel_MouseLeave" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp" >
                <Image Source="{Binding Converter={StaticResource IconImage}}" Margin="2,0,3,0" Height="16" Width="16" />
                <TextBlock Name="txtBlk" Text="{Binding NodeText}" />
            </StackPanel>
        </Border>
    </DataTemplate>
</ItemsControl.ItemTemplate>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肤浅与狂妄 2024-11-04 06:13:43

在发布问题之前,我实际上找到了解决方案。但我认为这对其他人可能有用。

我向 ScrollViewer 添加了 SizeChanged 处理程序。

SizeChanged="Scroller_SizeChanged"

在处理程序中,我添加了以下代码。

private void Scroller_SizeChanged(object sender, SizeChangedEventArgs e)
{
    WrapPanel wrpPanel = (WrapPanel)LicSwUserItemsCtl.GetItemsHost();
    wrpPanel.MaxHeight = ((ScrollViewer)sender).ActualHeight;
    wrpPanel.MaxWidth = ((ScrollViewer)sender).ActualWidth;
}

我很感兴趣是否可以通过绑定 xaml 中的 MaxWidth、MaxHeight 属性来实现这一点。

I found the solution actually before I posted my problem. But I think this might be useful for somebody else.

I added a SizeChanged handler to the ScrollViewer.

SizeChanged="Scroller_SizeChanged"

At the handler I added the following code.

private void Scroller_SizeChanged(object sender, SizeChangedEventArgs e)
{
    WrapPanel wrpPanel = (WrapPanel)LicSwUserItemsCtl.GetItemsHost();
    wrpPanel.MaxHeight = ((ScrollViewer)sender).ActualHeight;
    wrpPanel.MaxWidth = ((ScrollViewer)sender).ActualWidth;
}

I would be interested if this can be achieved by binding the MaxWidth, MaxHeight property in xaml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文