控制在第二个控件的一部分时不填充所有可用空间

时间:2014-10-02 08:29:10

标签: c# wpf winforms

我有一个用户控件,它包含一个显示一组图像的列表视图。这个列表视图效果很好!

<UserControl x:Class="ItemViewer.WPF.CustomControls.ctrlImageViewer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:ValueConverters="clr-namespace:ItemViewer.WPF.ValueConverters"
             xmlns:WPFHelper="clr-namespace:ItemViewer.WPF"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:ItemViewer.WPF.CustomControls"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d">

    <UserControl.Resources>
        <ValueConverters:FilenameValueConverter x:Key="imageConverter" />
        <ValueConverters:GroupNameConverter x:Key="groupNameConverter" />
    </UserControl.Resources>

    <UserControl.DataContext>
        <local:ImageCollectionVM />
    </UserControl.DataContext>

    <Grid x:Name="MainGrid">
        <!--  The list view containing the images, bound to the logos collection in the background  -->
        <ListView x:Name="ImagesListView"
                  Grid.Row="1"
                  Background="Transparent"
                  BorderThickness="0"
                  ItemsSource="{Binding ItemImages}"
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                  SelectedIndex="{Binding SelectedIndex,
                                          Mode=TwoWay,
                                          UpdateSourceTrigger=PropertyChanged}"
                  TabIndex="2">
            <!--
                Use a wrap panel so that the images appear side by side instead of one in each row
                We use a virtualising list view as there are enough images to slow down the performance
            -->
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <!--  We use a virtualising panel as there are too many images to be loaded in adequate amount of time  -->
                    <WPFHelper:VirtualizingWrapPanel />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

            <!--  Sets the template for the data to be displayed  -->
            <ListView.ItemTemplate>

                <DataTemplate>
                    <Border Margin="2">

                        <!--  Set the style  -->
                        <Border.Style>
                            <Style>
                                <Style.Triggers>
                                    <Trigger Property="UIElement.IsMouseOver" Value="True">
                                        <Setter Property="Border.BorderBrush" Value="LimeGreen" />
                                        <Setter Property="Border.BorderThickness" Value="3" />
                                        <Setter Property="Border.CornerRadius" Value="5" />
                                    </Trigger>
                                    <Trigger Property="UIElement.IsMouseOver" Value="False">
                                        <Setter Property="Border.BorderBrush" Value="LightSkyBlue" />
                                        <Setter Property="Border.BorderThickness" Value="2" />
                                        <Setter Property="Border.CornerRadius" Value="2" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Border.Style>

                        <StackPanel Background="White">
                            <!--  Defines the actual image being displayed  -->
                            <Image x:Name="ItemImageControl"
                                   Width="80"
                                   Height="150"
                                   Margin="1"
                                   HorizontalAlignment="Stretch"
                                   VerticalAlignment="Stretch"
                                   Cursor="Hand"
                                   Source="{Binding Converter={StaticResource imageConverter},
                                                    Mode=OneWay}" />

                            <TextBlock HorizontalAlignment="Center"
                                       FontWeight="Bold"
                                       Text="{Binding Converter={StaticResource groupNameConverter},
                                                      Mode=OneWay}" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</UserControl>

然后我将其中三个控件放到第二个WPF用户控件上,每个控件都在扩展器内,只有一个文本块来显示一些信息。

<UserControl x:Class="ItemViewer.WPF.CustomControls.ctrlImageCollection"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:CustomControls="clr-namespace:ItemViewer.WPF.CustomControls"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>

        <Expander Grid.Row="0"
                  Margin="4">
            <CustomControls:ctrlImageViewer x:Name="ImageViewer1" />
        </Expander>
        <Expander Grid.Row="1"
                  Margin="4">
            <CustomControls:ctrlImageViewer x:Name="ImageViewer2" />
        </Expander>
        <Expander Grid.Row="2"
                  Margin="4">
            <CustomControls:ctrlImageViewer x:Name="ImageViewer2" />
        </Expander>

        <StatusBar Grid.Row="4" Background="LightSkyBlue">
            <TextBlock Name="txtReference" />
        </StatusBar>
    </Grid>
</UserControl>

然后在Windows窗体项目中使用第二个控件,并将其停靠在窗体的左侧。

当我将图像添加到第一个用户控件之一时,当它是第二个用户控件的一部分时,图像就在那里,但列表视图不会扩展!

我需要列表ImageViewer(第一个控件)来扩展以填充所有可用空间,但目前它不是。

我想知道是否可以使用我从http://virtualwrappanel.codeplex.com/SourceControl/latest#VirtualizingWrapPanel.cs

获得的Virtualising Wrap Panel

有人可以帮我吗?

0 个答案:

没有答案