3

I have a typical MVVM structure. A mainwindow consists of some labels and a viewmodel (bound as ContentControl). This viewmodel has a listbox where I add entries. As the list grows the height of my viewmodel grows as well and thus the height of the whole application. Unfortunately the size doesn't stop at the edge of the screen and just grows out of the screen. I tried all the different size-restrictions on the mainwindow, the viewmodel is stretched in both dimensions. Furthermore, I tried to get a scrollbar for the listbox but didn't succeed (or it's just there but not enabled if I force it.)

How can I restrict the maximal size to be the resolution of the screen (fullscreen) and get a scrollbar for the listbox when the size is reached?

€: Okay, here should be the relevant part of my code. I tried a fixed size of 1024x768 but even that doesn't work.

MainWindow.xaml

<Window x:Class="SWS.MainWindow"
    DataContext="{Binding Main, Source={StaticResource Locator}}"
    Width="1024"
    Height="768"
    MaxWidth="1024"
    MaxHeight="768">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ContentControl Grid.Row="0" Content="{Binding CurrentViewModel}" />
    </Grid>
</Window>

The ViewModel in question

<UserControl x:Class="SWS.Views.ProgramView"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <DockPanel Grid.Row="0">
            <Label Width="65" Content="{x:Static p:Resources.PrV_Number}" />
            <Label Width="75" Content="{x:Static p:Resources.PrV_Weight}" />
            <Label Width="55" Content="{x:Static p:Resources.PrV_Action}" />
            <Label Content=" " />
        </DockPanel>
        <ListBox Grid.Row="1" 
                 VerticalAlignment="Stretch" 
                 ItemsSource="{Binding ParticleCollection}" 
                 ScrollViewer.CanContentScroll="True"
                 ScrollViewer.VerticalScrollBarVisibility="Auto">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <DockPanel>
                        <TextBlock Text="{Binding ID}" Width="53" />
                        <TextBlock Text="{Binding Weight, StringFormat=F4}" Width="65" />
                        <TextBlock Text="{Binding Action}" Width="52" />
                    </DockPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>
4

1 回答 1

4

您需要在WindowforContentControl和 your ListBoxin上将行高设置为 *UserControl以填充可用空间,而不是尝试获得所需的空间。

这很好用:

MainWindow.xaml:

<Window x:Class="SWS.MainWindow"
    DataContext="{Binding Main, Source={StaticResource Locator}}"
    Width="1024"
    Height="768"
    MaxWidth="1024"
    MaxHeight="768">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ContentControl Grid.Row="0" Content="{Binding CurrentViewModel}" />
    </Grid>
</Window>

UserControl

<UserControl x:Class="SWS.Views.ProgramView"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        ...
    </Grid>
</UserControl>

也可以通过更多方式做到这一点:

在你的Window你可以完全删除<Grid.RowDefinitions>...<Grid.RowDefinitions>。默认行为是您所寻找的。

当您有超过 1 行时,您可以选择指定Height="*"Height仅从RowDefinition

<Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition />
</Grid.RowDefinitions>
于 2013-03-25T19:15:23.923 回答