1
我想顯示整個列表框,當它的界限從主窗口溢出時。 DatePicker的行爲只是我想要做的(見附圖)。我該如何執行此操作?如何使溢出的內容像DatePicker一樣可見?
Screen capture describing what DatePicker can do and I can't do
<!-- 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;
}
}
}
,我相信它使用的是'Popup'控制。看到這個artical有關如何使用它的一些很好的信息https://wpf.2000things.com/tag/popup/ –
@BradleyUffner謝謝,它幫助了我很多:) – noobar