【问题标题】:How to make a ListView inside a grid in WPF fill the whole screen, even when resized如何使 WPF 中网格内的 ListView 填满整个屏幕,即使调整大小
【发布时间】:2012-07-17 19:10:06
【问题描述】:

我有一个控件,它是一个网格,其中第一行包含一些按钮,高度固定,第二行(也是最后一行)包含一个 ListView。然后我有一个主屏幕,它只包含一个菜单和上面的这个用户控件。我想将 ListView 的大小作为窗口的最大值,这意味着如果窗口最大化 ListView 会很大,以防窗口变小,ListView 也会变大(垂直滚动以帮助导航项目)。

这就是我的做法: 窗口:

<Window x:Class="TestUI.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Width="auto" Height="auto"
    MinHeight="300" MinWidth="300"
    Title="Test"
    DataContext="{Binding MainViewModel, Source={StaticResource Locator}}" >

<StackPanel Orientation="Vertical" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
    <DockPanel>
        <Menu DockPanel.Dock="Top" >
            <MenuItem Header="Item 1" Command="{Binding Command1}"/>
            <MenuItem Header="Item 2" Command="{Binding Command2}"/>
            <MenuItem Header="Item 3" Command="{Binding Command3}"/>
        </Menu>
    </DockPanel>
    <DockPanel LastChildFill="True">
        <ScrollViewer Content="{Binding Content}" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled" DockPanel.Dock="Bottom" />
    </DockPanel>
</StackPanel>
</Window>

用户控制:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="35" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0" Margin="5, 5, 5, 5" Orientation="Horizontal">
        <Button Content="New" Width="60" Command="{Binding NewCommand}"/>
        <Button Content="Remove" Width="60" Margin="10, 0, 0, 0" Command="{Binding RemoveCommand}" />
    </StackPanel>
    <DockPanel Grid.Row="1" Margin="5,5,5,5" LastChildFill="True" HorizontalAlignment="Stretch">
        <ListView x:Name="ListView" SelectionMode="Single" DockPanel.Dock="Top" ItemsSource="{Binding Entities}" SelectedItem="{Binding SelectedEntity}" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" VerticalAlignment="Stretch">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="auto" Header="Product" DisplayMemberBinding="{Binding Description}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </DockPanel>
</Grid>

你能帮帮我吗?

谢谢!

【问题讨论】:

    标签: wpf listview grid dockpanel


    【解决方案1】:

    我刚刚检查了您的用户控件,并且 Listview 对接工作正常。您可以使用背景颜色验证设计器中的布局。将列表视图的背景设置为蓝色表明它正在使用可用空间。

    我认为问题出在主窗体中的布局上,特别是 Stackpanel。这些因布局问题而臭名昭著,据我所知,您不需要它。使用 DockPanel 作为高级容器而不是多个停靠面板。 (这就是他们的目的)

    <DockPanel>
        <Menu DockPanel.Dock="Top" >
            <MenuItem Header="Item 1" Command="{Binding Command1}"/>
            <MenuItem Header="Item 2" Command="{Binding Command2}"/>
            <MenuItem Header="Item 3" Command="{Binding Command3}"/>
        </Menu>
        <ScrollViewer Content="{Binding Content}" VerticalScrollBarVisibility="Disabled"
             HorizontalScrollBarVisibility="Disabled" DockPanel.Dock="Fill" />
    </DockPanel>
    

    这会将您的菜单放在面板的顶部,并让包含用户控件(假设)的滚动查看器用完剩余的可用空间。

    堆栈面板仅适用于在一个区域内安排项目。如果您想使用布局来分配可用空间,请使用 DockPanels 或 Grids。

    【讨论】:

      【解决方案2】:

      您好,从 GridviewColumn 中移除 Width="Auto"。我希望这会有所帮助

      【讨论】:

      • 您在窗口中应用控件的位置。您必须检查的东西应该没有设置 UserControl 的 Height 和 Width 属性。并且在 Window 中,将此控件包含在 Grid 中,其中 Row of Height="Star" 和 Column of Width="Star"。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多