如何使溢出的内容像DatePicker一样可见?

时间:2016-05-27 02:55:41

标签: c# wpf

我希望在其边界从主窗口溢出时显示整个ListBox。 DatePicker只是我想做的事情(参见附图)。我该如何实现呢?

Screen capture describing what DatePicker can do and I can't do

enter image description here

<!-- MainWindow.xaml -->
<Window x:Class="OverflowSample.MainWindow"
        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:local="clr-namespace:OverflowSample"
        mc:Ignorable="d"
        Title="MainWindow" Height="130" Width="500">
    <Grid>
        <DatePicker HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
        <Button x:Name="buttonShowListBox" Content="Show" HorizontalAlignment="Left" Margin="240,10,0,0" VerticalAlignment="Top" Width="75" Click="buttonShowListBox_Click"/>
        <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="160" Margin="250,40,0,-90" VerticalAlignment="Top" Width="200" FontSize="24" Visibility="Collapsed">
            <ListBoxItem Content="Three"/>
            <ListBoxItem Content="Two"/>
            <ListBoxItem Content="One"/>
            <ListBoxItem Content="Zero"/>
        </ListBox>
    </Grid>
</Window>

// MainWindow.xaml.cs
using System.Windows;

namespace OverflowSample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void buttonShowListBox_Click(object sender, RoutedEventArgs e)
        {
            listBox.Visibility = (listBox.Visibility == Visibility.Visible) ?
                Visibility.Collapsed :
                Visibility.Visible;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

DatePicker使用Popup并将日历放入其中。

.net框架源可供您自己查看: http://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/DatePicker.cs,c20427230c18ba13