Wpf 使用scrollviewer的动态高度

Wpf 使用scrollviewer的动态高度,wpf,scrollviewer,Wpf,Scrollviewer,我有一个带有scrollviewer的窗口,scrollviewer是一个usecontrols集合,可以从完全空到无限大。我希望窗口的高度与屏幕的高度相匹配,这样它就不会溢出,我还希望没有使用代码隐藏,因为我使用的是MVVM模式 谢谢 <Window x:Class="FreePIE.GUI.Shells.CurveSettingsView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

我有一个带有scrollviewer的窗口,scrollviewer是一个usecontrols集合,可以从完全空到无限大。我希望窗口的高度与屏幕的高度相匹配,这样它就不会溢出,我还希望没有使用代码隐藏,因为我使用的是MVVM模式

谢谢

<Window x:Class="FreePIE.GUI.Shells.CurveSettingsView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CurveSettingsView" Background="{DynamicResource WindowBackgroundBrush}" SizeToContent="WidthAndHeight" MinHeight="200" MinWidth="200">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ScrollViewer>
            <ItemsControl x:Name="Curves" Grid.Row="0"></ItemsControl>
        </ScrollViewer>
        <Button x:Name="AddCurve" Width="150" Grid.Row="1">Add new curve</Button>
    </Grid>
</Window>

添加新曲线

如果我理解正确,您希望您的
ItemsControl
占用所有可用空间,并调整其内容大小,以便无需ScrollViewer即可查看所有项目

如果是这种情况,请将
ItemsControl
中的默认
ItemsPanelTemplate
替换为一个扩展以占用所有可用空间的控件,例如
DockPanel
。默认的
ItemsPanelTemplate
是一个
StackPanel
,它将根据需要增长

<ItemsControl x:Name="Curves" Grid.Row="0">

    <!-- ItemsPanelTemplate -->
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <DockPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <!-- ItemContainerStyle -->
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="DockPanel.Dock" Value="Top" />
        </Style>
    </ItemsControl.ItemContainerStyle>

</ItemsControl>

抱歉,如果我有点不清楚,不,我希望窗口占用的空间不超过屏幕的高度,如果itemscontrol的内容使窗口比屏幕大,那么我希望scrollviewer限制itemscontrol的可见部分,以便所有窗口在屏幕上都可见,谢谢。。。
<ItemsControl x:Name="Curves" Grid.Row="0">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="{Binding 
                RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, 
                Path=Items.Count}" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>