listbox

How can I add a context menu to a ListBoxItem?

狂风中的少年 提交于 2019-12-03 10:39:29
问题 I have a ListBox and I want to add a context menu to each item in the list. I've seen the "solution" to have the right click select an item and suppress the context menu if on white space, but this solution feels dirty. Does anyone know a better way? 回答1: This way the menu will pop up next to the mouse private string _selectedMenuItem; private readonly ContextMenuStrip collectionRoundMenuStrip; public Form1() { var toolStripMenuItem1 = new ToolStripMenuItem {Text = "Copy CR Name"};

WPF Listbox with touch inertia pulls down entire window

徘徊边缘 提交于 2019-12-03 10:09:46
I have a full screen WPF application built for a touch monitor, and I have some Listbox s on the main screen. When I flick the 'Listbox' it scrolls fine, but when it gets to the end of the list, the entire application gets pulled down from the top of the screen, can I stop this behavior somehow? Has anyone else seen this? Yes, that default behaviour of the ListBox (or rather, the ScrollViewer inside the default ListBox template) is weird - when I first came across it, I thought it must be a practical joke. In fact, it's really hard to find any documentation about it - but it is briefly

Saving Listbox Data to XML?

梦想与她 提交于 2019-12-03 08:50:44
I have 2 listboxes, the first listbox stores data pointers for each items Object property (defined by a custom class I have written). Whenever I select an item from this listbox, I populate the second listbox by accessing some of the data stored on the first listbox. That is all good, but now I need to know how to save and restore the listboxes to XML. I would appreciate it if someone could provide an example or assist me in writing the code to do this. Here is some sample code to show how I create and access the data: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes,

C# Listbox set selected item

大城市里の小女人 提交于 2019-12-03 08:06:58
i have a C# listbox with the values Profile 1 Profile 2 Profile 3 I want to have Profile 2 selected when the form loads. How do I do this? Set the ListBox.SelectedIndex property in the Form.Shown event. For example: public Form1() { InitializeComponent(); // Adding the event handler in the constructor this.Shown += new EventHandler(Form1_Shown); } private void Form1_Shown(object sender, EventArgs e) { myListBox.SelectedIndex = 1; } Put following code in Form.Loaded event: listBox1.SelectedItem = "Profile 2"; listBox1.Items.Add("Profile 1"); listBox1.Items.Add("Profile 2"); listBox1.Items.Add(

PreviewKeyDown for Windows Store App ListBox

做~自己de王妃 提交于 2019-12-03 08:05:43
Is there an equivalent to the PreviewKeyDown for a Windows Store App ? It isn't available. I have exactly the same problem as described here: I have a ListBox with a TextBox above it. I would like to use the arrow keys to navigate from the ListBox to the TextBox. The intention is that if the first item in the ListBox is selected, and the user keys up, the TextBox will get focus. Ah, tricky. Handling key events isn't super-obvious. Here's what you want: public MainPage() { this.InitializeComponent(); Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += (s, args) => { if ((args

Improve performance for huge ListBox in StackPanel?

本小妞迷上赌 提交于 2019-12-03 07:43:52
I am using a StackPanel to layout several controls vertically (ie, Title, sub titles, listbox, separator, listbox, etc). The StackPanel is a child of a ScrollViewer to ensure its content is always scrollable. One of the controls in the StackPanel is a ListBox. Its ItemsSource is data bound to a huge collection, and a complex DataTemplate is used to realise each item. Unfortunately, I'm getting really poor performance (high cpu/memory) with it. I tried setting the ListBox's ItemsPanel to a VirtualizingStackPanel, and overriding its ControlTemplate to only an ItemsPresenter (remove the ListBox's

WPF: Binding to ListBoxItem.IsSelected doesn't work for off-screen items

耗尽温柔 提交于 2019-12-03 06:44:20
In my program I have a set of view-model objects to represent items in a ListBox (multi-select is allowed). The viewmodel has an IsSelected property that I would like to bind to the ListBox so that selection state is managed in the viewmodel rather than in the listbox itself. However, apparently the ListBox doesn't maintain bindings for most of the off-screen items, so in general the IsSelected property is not synchronized correctly. Here is some code that demonstrates the problem. First XAML: <StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock>Number of selected items: </TextBlock>

How do I bind the result of DataTable.Select() to a ListBox control?

前提是你 提交于 2019-12-03 06:42:06
I have the following code: ListBox.DataSource = DataSet.Tables("table_name").Select("some_criteria = match") ListBox.DisplayMember = "name" The DataTable.Select() method returns an array of System.Data.DataRow objects. No matter what I specify in the ListBox.DisplayMember property, all I see is the ListBox with the correct number of items all showing as System.Data.DataRow instead of the value I want which is in the "name" column! Is it possible to bind to the resulting array from DataTable.Select() , instead of looping through it and adding each one to the ListBox ? (I've no problem with

WPF Memory Usage

早过忘川 提交于 2019-12-03 06:39:53
Application: WPF Application consisting of a textbox on top and a listbox below Users type a string in the TextBox to find employees, and search results are displayed in the ListBox ListBox uses DataTemplates to display elements (shows employee name, department, phone and a thumbnail picture.) Implementation: At application startup I query the database and retrieve all employees and related information to be shown in the ListBox. This is kept in memory the entire time. After application startup, all the searchable data is in memory and searches are virtually instantaneous. All searches are

Autoscrolling a Listbox under a certain situation

雨燕双飞 提交于 2019-12-03 06:29:34
How to autoscroll a Listbox after adding a new item, but only if the scrollbar is at the bottom before the item is added? You can just try it : listBox1.SelectedIndex = listBox1.Items.Count - 1; listBox1.SelectedIndex = -1; This sample code should help you out. I've done this many times with a TextBox but it took awhile to figure it out for a ListBox Obviously, it's just a Form with a Button and a ListBox. Modify to fit your needs: private void button1_Click(object sender, EventArgs e) { listBox1.Items.Add("Some Text " + listBox1.Items.Count.ToString()); //The max number of items that the