WPF - 主ScrollViewer不应禁用usercontrol中列表框的scrollviewer

时间:2014-08-26 15:28:36

标签: c# wpf listbox scrollviewer

我有以下情况。主窗口有一个ScrollViewer,它托管了几个用户控件,并设置为Horizo​​ntalScrollBarVisibility =" Auto"。其中一个包含带有水平项的ListBox。

Schematic application setup

如果UserControl3填充了ListBox,则主ScrollViewer会显示水平ScrollBar,UserControl3会水平增长。

这不是我想要的。我想,UserControl3总是完全适合MainContainer,ListBox应该显示自己的ScrollBar。

我该怎么做?

任何提示都是适用的。 最好的问候

更新 - 这是代码

<Window x:Class="TimlineStudy.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:TimlineStudy="clr-namespace:TimlineStudy"
    xmlns:view="clr-namespace:VMS.Timeline.Common.UI.View;assembly=VMS.Timeline.Common.UI"
    Title="MainWindow"
    Width="525"
    Height="350">

<TabControl Grid.Row="1">
    <TabItem Header="Timeline with scrollviewer">
        <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <TimlineStudy:ScrollableUserControl x:Name="ScrollableUserControl" />
        </ScrollViewer>
    </TabItem>

    <TabItem Header="Timeline without ScrollViewer">
        <TimlineStudy:ScrollableUserControl x:Name="ScrollableUserControlWithoutScrollViewerAround" />
    </TabItem>
</TabControl>
</Window>

<UserControl x:Class="TimlineStudy.ScrollableUserControl"
         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"
         xmlns:viewModel="clr-namespace:TimlineStudy.ViewModel"
         d:DataContext="{d:DesignInstance viewModel:ScrollableUserControlViewModel}"
         d:DesignHeight="300"
         d:DesignWidth="50"
         mc:Ignorable="d">

<Grid x:Name="GridLayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="25" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Grid x:Name="GridToolBar" Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>

        <Label x:Name="LabelNumberOfItemsToAdd" Grid.Column="0">Number of items to add:</Label>
        <TextBox x:Name="TextBoxItemsToAdd"
                 Grid.Column="1"
                 Text="{Binding NumberOfItemsToAdd,
                                Mode=TwoWay}" />
        <Button x:Name="ButtonCreateTimelineItems"
                Grid.Column="2"
                Command="{Binding CreateTimeLineItemsCommand}">
            Add TimeLine Items
        </Button>
        <Button x:Name="ButtonClearTimelineItems"
                Grid.Column="3"
                Command="{Binding ClearTimeLineItemsCommand}">
            Clear TimeLine Items
        </Button>
    </Grid>

    <ListBox x:Name="MyItemsListBox" 
             Grid.Row="1"
             ItemTemplate="{StaticResource TimelineItemTemplate}"
             ItemsSource="{Binding TimeLineItems}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
</Grid>
</UserControl>

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:model="clr-namespace:TimlineStudy.Model">

<DataTemplate x:Key="TimelineItemTemplate" DataType="{x:Type model:TimeLineItem}">
    <Border x:Name="BorderLayoutRoot"
            BorderBrush="Red"
            BorderThickness="1">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="2*" />
                <ColumnDefinition Width="8*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="25" />
                <RowDefinition Height="25" />
            </Grid.RowDefinitions>

            <Label Grid.Row="0" Grid.Column="0">Item name:</Label>
            <TextBlock Grid.Row="0"
                       Grid.Column="1"
                       Text="{Binding ItemName}" />


            <Label Grid.Row="1" Grid.Column="0">Item index:</Label>
            <TextBlock Grid.Row="1"
                       Grid.Column="1"
                       Text="{Binding ItemIndex}" />
        </Grid>
    </Border>
</DataTemplate>

</ResourceDictionary>

1 个答案:

答案 0 :(得分:1)

如果你不介意一些代码隐藏,你可以在SizeChanged事件中处理它:

XAML:

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" SizeChanged="ScrollViewer_SizeChanged">
    <TimlineStudy:ScrollableUserControl x:Name="ScrollableUserControl" />
</ScrollViewer>

代码隐藏:

private void ScrollViewer_SizeChanged(object sender, SizeChangedEventArgs e)
{
    ScrollableUserControl.MaxWidth = e.NewSize.Width; // Subtract left and right margin if needed
}