如何在ComboBox中垂直制作列表框在WPF中可滚动

时间:2012-02-14 08:35:07

标签: c# .net wpf xaml

请大师,我在我的wpf应用程序中有ComboBoxes,并且能够将包含CheckBoxes的ListBox添加到其中,以便用户可以从一个类别中选择多个项目。但现在的问题是:

** ListBoxes无法显示整个CheckBox,因为尽管将高度设置为Auto,它们仍拒绝滚动。

感谢您在考虑本问题时提前一段时间

* 以下样式适用于其中一个CombBox,因为s ame步骤适用于其余部分*

          <ComboBox Height="23" HorizontalAlignment="Left" Margin="20,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" Text="Hamburger & Sandwiches" StaysOpenOnEdit="True">

     <ListBox BorderThickness="0,0,2,0" ScrollViewer.VerticalScrollBarVisibility="Visible"
                SelectionMode="Multiple" Background="#E3418D00" Focusable="False" SnapsToDevicePixels="True"> 
                    <ListBox.BorderBrush>
                    <SolidColorBrush />
                </ListBox.BorderBrush>
                <CheckBox Foreground="Cyan" FontFamily="Lucida Sans" Content="Chicken Wrap" ClickMode="Release" BorderBrush="#FFF58B09" Name="chkChickenWrap">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkRibeyeSteakWrap" Content="Ribeye Steak Wrap" FontFamily="Lucida Sans" Foreground="Cyan" BorderBrush="Orange">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkShreddedBeefWrap" Foreground="Cyan" FontFamily="Lucida Sans" Content="Shredded Beef Wrap" BorderBrush="Orange">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkCheeseburgerParadiseWrap" Foreground="Cyan" FontFamily="Lucida Sans" BorderBrush="Orange" Content="Cheeseburger Paradise Wrap">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkVeggieWrap" BorderBrush="Orange" Content="Veggie Wrap" Foreground="Cyan" FontFamily="Lucida Sans">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkChiliBurger" Foreground="Cyan" FontFamily="Lucida Sans" Content="Chili Burger" BorderBrush="Orange">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkTiajuanaTorpedo" BorderBrush="Orange" Content="Tiajuana Torpedo" FontFamily="Lucida Sans" Foreground="Cyan">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Foreground="Cyan" FontFamily="Lucida Sans" Content="Stuffed Burger" BorderBrush="Orange" Name="chkStuffedBurger">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkCheeseBurger" BorderBrush="Orange" Content="Cheese Burger" Foreground="Cyan" FontFamily="Lucida Sans">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Foreground="Cyan" FontFamily="Lucida Sans" Content="Patty Melt" BorderBrush="Orange" Name="chkPattyMelt">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Foreground="Cyan" FontFamily="Lucida Sans" Content="Harry's Big Barn Burger" BorderBrush="Orange" Name="chkHarrysBigBarnBurger">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkTheReuban" BorderBrush="Orange" Content="The Reuban" FontFamily="Lucida Sans" Foreground="Cyan">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkBBQBeefSandwich" Foreground="Cyan" FontFamily="Lucida Sans" Content="BBQ Beef Sandwich" BorderBrush="Orange">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkSteakSandwich" BorderBrush="Orange" Content="Steak Sandwich" FontFamily="Lucida Sans" Foreground="Cyan">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkFrenchDip" BorderBrush="Orange" Content="French Dip" FontFamily="Lucid sans" Foreground="Cyan">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Foreground="Cyan" FontFamily="Lucida Sans" Content="Chili Dog" BorderBrush="Orange" Name="chkChiliDog">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkChickenFriedSteakSandwich" BorderBrush="Orange" Content="Chicken Fried Steak Sandwich" FontFamily="Lucida Sans" Foreground="Cyan">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkClubSandwich" BorderBrush="Orange" Content="Club Sandwich" Foreground="Cyan" FontFamily="Lucida Sans">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkCajunCatfishSandwich" Foreground="Cyan" FontFamily="Lucida Sans" Content="Cajun Catfish Sandwich" BorderBrush="Orange">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkEggAndCucumberTeaSandwich" BorderBrush="Orange" Content="Egg &amp; Cucumber Tea Sandwiches" FontFamily="Lucid sans" Foreground="Cyan">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
                <CheckBox Name="chkSmokedSalmonCrosini" BorderBrush="Orange" Content="Smoked Salmon Crosini Recipe" FontFamily="Lucida Sans" Foreground="Cyan">
                    <CheckBox.Background>
                        <SolidColorBrush />
                    </CheckBox.Background>
                </CheckBox>
            </ListBox>
        </ComboBox>

2 个答案:

答案 0 :(得分:1)

使用Scrollviewer。

将所有可滚动控件放在scrollviewer中。

答案 1 :(得分:1)

要垂直滚动ListBox项目,您可能需要为项目面板指定最大高度。

这段代码将完全符合您的要求:

<ComboBox Height="23" HorizontalAlignment="Left" Margin="20,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" Text="Hamburger and Sandwiches">
    <ListBox BorderThickness="0,0,2,0" ScrollViewer.VerticalScrollBarVisibility="Visible"
        SelectionMode="Multiple" Background="#E3418D00" Focusable="False" SnapsToDevicePixels="True">

        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical" MaxHeight="200"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <ListBox.BorderBrush>
            <SolidColorBrush />
        </ListBox.BorderBrush>
        <CheckBox Foreground="Cyan" FontFamily="Lucida Sans" Content="Chicken Wrap" ClickMode="Release" BorderBrush="#FFF58B09" Name="chkChickenWrap">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkRibeyeSteakWrap" Content="Ribeye Steak Wrap" FontFamily="Lucida Sans" Foreground="Cyan" BorderBrush="Orange">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkShreddedBeefWrap" Foreground="Cyan" FontFamily="Lucida Sans" Content="Shredded Beef Wrap" BorderBrush="Orange">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkCheeseburgerParadiseWrap" Foreground="Cyan" FontFamily="Lucida Sans" BorderBrush="Orange" Content="Cheeseburger Paradise Wrap">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkVeggieWrap" BorderBrush="Orange" Content="Veggie Wrap" Foreground="Cyan" FontFamily="Lucida Sans">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkChiliBurger" Foreground="Cyan" FontFamily="Lucida Sans" Content="Chili Burger" BorderBrush="Orange">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkTiajuanaTorpedo" BorderBrush="Orange" Content="Tiajuana Torpedo" FontFamily="Lucida Sans" Foreground="Cyan">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Foreground="Cyan" FontFamily="Lucida Sans" Content="Stuffed Burger" BorderBrush="Orange" Name="chkStuffedBurger">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkCheeseBurger" BorderBrush="Orange" Content="Cheese Burger" Foreground="Cyan" FontFamily="Lucida Sans">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Foreground="Cyan" FontFamily="Lucida Sans" Content="Patty Melt" BorderBrush="Orange" Name="chkPattyMelt">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Foreground="Cyan" FontFamily="Lucida Sans" Content="Harry's Big Barn Burger" BorderBrush="Orange" Name="chkHarrysBigBarnBurger">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkTheReuban" BorderBrush="Orange" Content="The Reuban" FontFamily="Lucida Sans" Foreground="Cyan">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkBBQBeefSandwich" Foreground="Cyan" FontFamily="Lucida Sans" Content="BBQ Beef Sandwich" BorderBrush="Orange">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkSteakSandwich" BorderBrush="Orange" Content="Steak Sandwich" FontFamily="Lucida Sans" Foreground="Cyan">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkFrenchDip" BorderBrush="Orange" Content="French Dip" FontFamily="Lucid sans" Foreground="Cyan">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Foreground="Cyan" FontFamily="Lucida Sans" Content="Chili Dog" BorderBrush="Orange" Name="chkChiliDog">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkChickenFriedSteakSandwich" BorderBrush="Orange" Content="Chicken Fried Steak Sandwich" FontFamily="Lucida Sans" Foreground="Cyan">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkClubSandwich" BorderBrush="Orange" Content="Club Sandwich" Foreground="Cyan" FontFamily="Lucida Sans">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkCajunCatfishSandwich" Foreground="Cyan" FontFamily="Lucida Sans" Content="Cajun Catfish Sandwich" BorderBrush="Orange">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkEggAndCucumberTeaSandwich" BorderBrush="Orange" Content="Egg &amp; Cucumber Tea Sandwiches" FontFamily="Lucid sans" Foreground="Cyan">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
        <CheckBox Name="chkSmokedSalmonCrosini" BorderBrush="Orange" Content="Smoked Salmon Crosini Recipe" FontFamily="Lucida Sans" Foreground="Cyan">
            <CheckBox.Background>
                <SolidColorBrush />
            </CheckBox.Background>
        </CheckBox>
    </ListBox>
</ComboBox>