Giải pháp cho WPF
Part 1
Nguyên tắc, bạn có thể sử dụng phong cách và mẫu cho việc thực hiện các câu hỏi của bạn. Trong các ComboBox
kết quả được đưa ra trong Popup
, nhưng mặc định nó không hỗ trợ thay đổi kích thước. Thêm thay đổi kích thước không khó, nếu bạn sử dụng sự kiện DragDelta
. Ví dụ:
private void MyThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
double yadjust = MyPopup.Height + e.VerticalChange;
double xadjust = MyPopup.Width + e.HorizontalChange;
if ((xadjust >= 0) && (yadjust >= 0))
{
MyPopup.Width = xadjust;
MyPopup.Height = yadjust;
}
}
Sự kiện này là tốt hơn để thiết lập trên Thumb
điều khiển (Ông cũng có sự kiện DragStarted
, DragCompleted
).
Tất cả đều rất tốt, nhưng chúng tôi cần thực hiện bên trong ComboBox
. Một cách là sử dụng Style
và Template
. Để bắt đầu, thêm một Thumb
trong phong cách ComboBox
để nó xuất hiện trong danh sách mở rộng:
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid Name="MainGrid">
<ToggleButton Name="ToggleButton" Template="{StaticResource ComboBoxToggleButton}" Grid.Column="2" Focusable="False" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" />
<ContentPresenter Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" />
<TextBox x:Name="PART_EditableTextBox" Style="{x:Null}" Template="{StaticResource ComboBoxTextBox}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="3,3,23,3" Focusable="True" Background="{TemplateBinding Background}" Visibility="Hidden" IsReadOnly="{TemplateBinding IsReadOnly}" />
<!-- Expanded list store here -->
<Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
<Grid Name="DropDown" Width="100" Height="100" SnapsToDevicePixels="True">
<Border x:Name="DropDownBorder" Background="White" BorderThickness="1" BorderBrush="Gray" />
<ScrollViewer Margin="2" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
<!-- Our Thumb -->
<Thumb x:Name="ResizeGripThumb" Style="{StaticResource ResizeGripStyle}" HorizontalAlignment="Right" Margin="0,0,2,2" Background="Transparent" VerticalAlignment="Bottom" Width="12" Height="12" />
</Grid>
</Popup>
</Grid>
...
Đối với màn hình bình thường Thumb
, thêm vào phong cách nó với một Path
:
<!-- ResizeGrip Style -->
<Style x:Key="ResizeGripStyle" TargetType="{x:Type Thumb}">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Cursor" Value="SizeNWSE" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Grid>
<Path Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Stretch="Fill" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Fill="Gray" Data="M8,0L10,0 10,2 8,2z M4,4L6,4 6,6 4,6z M8,4L10,4 10,6 8,6z M0,8L2,8 2,10 0,10z M4,8L6,8 6,10 4,10z M8,8L10,8 10,10 8,10z "/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Bây giờ, ở giai đoạn này, chúng tôi đã hiển thị ResizeGrip
trong danh sách mở rộng. Tuy nhiên, mặc định ScrollBar
, sau đó đóng nó với sự hiện diện của mình, vì vậy nó cũng xác định phong cách cho ScrollBar
. Nó sẽ thay đổi lề VerticalThumb
, do đó:
...
<!-- VerticalThumb for ScollBar -->
<Style x:Key="VerticalThumb" TargetType="{x:Type Thumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Rectangle Fill="Gray" Margin="-1,-1,-3,16" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Bây giờ, có một màn hình hiển thị bình thường của các thành phần chính.Tuyên bố ComboBox
trong XAML:
<ComboBox Name="ResizeComboBox" Style="{StaticResource MyComboBox}" IsEditable="True" IsTextSearchEnabled="True" FontSize="14" SelectedIndex="0" Width="100" Height="30">
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
<ComboBoxItem>3</ComboBoxItem>
<ComboBoxItem>4</ComboBoxItem>
<ComboBoxItem>5</ComboBoxItem>
<ComboBoxItem>6</ComboBoxItem>
<ComboBoxItem>7</ComboBoxItem>
<ComboBoxItem>8</ComboBoxItem>
</ComboBox>
Nó vẫn còn để thiết lập một handler cho thay đổi kích thước Popup
. Tôi sẽ làm cho nó kiểm soát tìm kiếm trong mẫu bằng cách sử dụng hàm FindChild<T>
. Để được an toàn, tôi sẽ làm điều đó trong trường hợp ContentRendered
của Window
, để biết rằng tất cả các yếu tố nạp:
private void Window_ContentRendered(object sender, EventArgs e)
{
// Find MainGrid in our ComboBox template
Grid MyMainGrid = FindChild<Grid>(ResizeComboBox, "MainGrid");
// Find Popup in Grid
Popup MyPopup = MyMainGrid.FindName("Popup") as Popup;
// Find Thumb in Popup
Thumb MyThumb = MyPopup.FindName("ResizeGripThumb") as Thumb;
// Set the handler
MyThumb.DragDelta += new DragDeltaEventHandler(MyThumb_DragDelta);
}
Danh bạ nhà FindChild<>
:
public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
{
if (parent == null)
{
return null;
}
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
T childType = child as T;
if (childType == null)
{
foundChild = FindChild<T>(child, childName);
if (foundChild != null) break;
}
else
if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
if (frameworkElement != null && frameworkElement.Name == childName)
{
foundChild = (T)child;
break;
}
else
{
foundChild = FindChild<T>(child, childName);
if (foundChild != null)
{
break;
}
}
}
else
{
foundChild = (T)child;
break;
}
}
return foundChild;
}
Danh bạ nhà handler MyThumb_DragDelta
:
private void MyThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Thumb MyThumb = sender as Thumb;
Grid MyGrid = MyThumb.Parent as Grid;
// Set the new Width and Height fo Grid, Popup they will inherit
double yAdjust = MyGrid.Height + e.VerticalChange;
double xAdjust = MyGrid.Width + e.HorizontalChange;
// Set new Height and Width
if ((xAdjust >= 0) && (yAdjust >= 0))
{
MyGrid.Width = xAdjust;
MyGrid.Height = yAdjust;
}
}
Nó như vậy:

Some notes:
Để đặt giá trị mẫu, chúng phải có giá trị mặc định hoặc giá trị sẽ là NaN
và chúng tôi không thể đặt chúng. Chúng tôi có những thông số được thiết lập ở đây:
<Grid Name="DropDown" Width="100" Height="100" SnapsToDevicePixels="True">
Một danh sách đầy đủ các mẫu và mã có thể được tìm thấy here, vì chúng là lớn về khối lượng. Phong cách không thể dễ dàng thay đổi, bởi vì họ đã được thực hiện trong một vội vàng, vì vậy họ nên làm cho mình.
Part 2
Như để lưu trữ các dữ liệu nhập vào, nó phụ thuộc vào mục tiêu của mình. Tôi nghĩ bạn có thể làm một việc như sau:
- Tạo danh sách (có thể là
ObservableCollection
) để lưu trữ các mục.
- Sau khi nhập thành công phần tử, ví dụ:
which he was found
ở một số nguồn, hãy lưu nó vào danh sách của bạn.
- Gắn danh sách này vào
ComboBox
.
Chỉ cần thiết lập các thuộc tính IsEditable
= "True"
và IsTextSearchEnabled
= "True"
để hiển thị các ký tự đầu vào trong danh sách thả xuống (như trong ví dụ của tôi).
Vì vậy, bạn sẽ có danh sách trong đó các thành phần được thêm vào, có thể được hiển thị cho người dùng.
danh sách hoàn là thay đổi kích thước trong Windows 8 chỉ. – Vladimir