我试图让类似Windows资源管理器使用的Windows Vista / 7痕迹导航栏的用户控件。
I'm trying to make a user control similar to the Windows Vista/7 breadcrumb bar used in windows explorer.
然而,当我表现出的下拉菜单中有许多子项的痕迹,我得到一个很长的名单,有时超过了屏幕尺寸。 在Windows Vista / 7的但是例如,有一个最大的18个项目同时显示,并滚动条出现在当分项数超过此数(18)的权利。
However, when I show the drop down menu for a breadcrumb with many sub items, I get a very long list that sometimes exceeds the screen size. In the Windows Vista/7 example however, there are a maximum of 18 items displayed at a time and a scrollbar appears at the right when the number of sub items exceeds this number (18).
我想知道是否有任何人知道的方式来复制微软一样。 [即,如何放置一个下拉与自动滚动功能的控制菜单。] 谢谢。 亚历克斯
I wanted to know if anyone out there knows a way to replicate what Microsoft does. [That is, how to place a drop down menu in a control with auto-scroll capability.] Thanks. Alex
的Windows 7 / Vista的痕迹看起来类似的列表视图。 下图给出了我的意思(名单出现点击按钮)为例(在Windows XP中):
Windows 7/Vista breadcrumb looks similar to a list view. The following picture gives an example (on windows xp) of what I mean (the list appears clicking on the button):
和这里的code获得它:
and here's the code to get it:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var button = sender as Button;
// create fake items list
List<string> strings = new List<string>();
for (int i = 0; i < 36; i++)
strings.Add("ITEM " + (i+1));
var listViewItems = strings.Select(x => new ListViewItem(x, 0)).ToArray();
// create a new list view
ListView listView = new ListView();
listView.View = View.SmallIcon;
listView.SmallImageList = imageList1;
listView.MultiSelect = false;
// add items to listview
listView.Items.AddRange(listViewItems);
// calculate size of list from the listViewItems' height
int itemToShow = 18;
var lastItemToShow = listViewItems.Take(itemToShow).Last();
int height = lastItemToShow.Bounds.Bottom + listView.Margin.Top;
listView.Height = height;
// create a new popup and add the list view to it
var popup = new ToolStripDropDown();
popup.AutoSize = false;
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(listView);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
host.AutoSize = false;
host.Size = listView.Size;
popup.Size = listView.Size;
popup.Items.Add(host);
// show the popup
popup.Show(this, button.Left, button.Bottom);
}
}
编辑:
要获得所选择的项目,一个方法如下:
To get the selected item, one way is the following:
// change some properties (for selection) and subscribe the ItemActivate
// event of the listView
listView.HotTracking = true;
listView.Activation = ItemActivation.OneClick;
listView.ItemActivate += new EventHandler(listView_ItemActivate);
// the click on the item invokes this method
void listView_ItemActivate(object sender, EventArgs e)
{
var listview = sender as ListView;
var item = listview.SelectedItems[0].ToString();
var dropdown = listview.Parent as ToolStripDropDown;
// unsubscribe the event (to avoid memory leaks)
listview.SelectedIndexChanged -= listView_ItemActivate;
// close the dropdown (if you want)
dropdown.Close();
// do whatever you want with the item
MessageBox.Show("Selected item is: " + item);
}