listbox

WPF Listbox + Expander events

点点圈 提交于 2019-12-04 12:58:50
问题 I have an Expander in the ItemTemplate of a ListBox. Renders fine. The issue I have run into is that I would like the ListBox_SelectionChanged event to fire when the expander is expanded and/or selected. The MouseDown event does not seem to bubble up to the ListBox. What I need is the SelectedIndex of the ListBox. Because the ListBox_SelectionChanged does not get fired, the index is -1 and I cannot determine which item has been selected. The ListBox_SelectionChanged Event is fired if a user

C# Listbox set selected item

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 12:36:43
问题 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? 回答1: 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; } 回答2: Put following code in Form.Loaded event: listBox1

Can I get a WPF ListBox to inherit brushes from parent element?

安稳与你 提交于 2019-12-04 12:29:12
My WPF window has its foreground brush set to a brush from a resource dictionary, and I want all text in the window to have this color, so I don't touch the foreground brush in anything else. Textboxes get the color Textblocks get the color Buttons get the color Listboxes do not get the color, and so neither do their contents. Is there any way to get a Listbox to behave like the other controls in this respect? Assuming not, and that this is by design, what is the rationale? Edit: It seems my question is not clear enough. I understand how to create styles and resources and apply them to ListBox

Prevent WPF ListBox from selecting item under mouse when layout changes

浪尽此生 提交于 2019-12-04 11:14:43
If a WPF ListBox gets a MouseMove event while the mouse button is held down, it will change the listbox's selection. That is, if you click the mouse on item #1, and then drag over item #2, it will deselect item #1 and select item #2 instead. How can I prevent this? That's the short version. The slightly longer version is this: When the user double-clicks an item in my ListBox, I make other changes to my layout, which includes showing other controls above the ListBox. This moves the ListBox downwards, which means the mouse is now positioned over a different ListBoxItem than it was when the user

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

馋奶兔 提交于 2019-12-04 11:02:17
问题 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

How to display items horizontally in a listbox control?

懵懂的女人 提交于 2019-12-04 10:41:49
I am developing window phone 7 application. I am new to the window phone 7 application. I have the following listbox control in my application <ListBox Margin="0,355,70,205" Name="WeekSummaryListBox" DataContext="{Binding}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock TextWrapping="Wrap" Width="150" Text="{Binding Amount}" Foreground="Orange"></TextBlock> <TextBlock TextWrapping="Wrap" Width="150" Text="{Binding Currency}" Foreground="Orange"></TextBlock> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> <ListBox.Template> <ControlTemplate>

How do I bind to a ListBox in IronPython?

人盡茶涼 提交于 2019-12-04 10:41:11
I am just starting out using IronPython with WPF and I don't quiet understand how binding is supposed to be done. Normally in WPF I would just do something like this: <ListBox Name="MyListBox"> <ListBox.Resources> <Style TargetType="ListBoxItem"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <DockPanel> <TextBlock Text="{Binding Path=From}" /> <TextBlock Text="{Binding Path=Subject}" /> </DockPanel> </DataTemplate> </Setter.Value> </Setter> </Style> </ListBox.Resources> </ListBox> Then in my code behind: MyListBox.ItemsSource = new ObservableCollection<Email>() But in

Change color of specific item on listbox that contains a specific string on drawitem

时间秒杀一切 提交于 2019-12-04 10:11:12
i want to change the color of item that contains a specific string Private Sub ListBox2_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox2.DrawItem e.DrawBackground() If DrawItemState.Selected.ToString.Contains("specific string") Then e.Graphics.FillRectangle(Brushes.LightGreen, e.Bounds) End If e.DrawFocusRectangle() that is my code but not working Alright, first you need to set the property DrawMode of the list box to "OwnerDrawFixed" instead of Normal. Otherwise you will never get the DrawItem event to fire. When that is done it is all

WPF - Why Listbox items do not fill uniformgrid

不打扰是莪最后的温柔 提交于 2019-12-04 10:01:11
I have a listbox with the ItemsPanelTemplate set to UniformGrid with rows= 6 and cols= 7. I want the listbox items to fill their space. I am using a datatemplete defined in a dictionary. The outer control of my template is a Border with HorizontalAlignment=Stretch and VerticalAlignent=Strectch but the templates do not fill the listbox items space? Any ideas? Malcolm The answer to this to set HorizontalContentAligment and VerticalContentAlignment to Stretch on the LISTBOX not the datatemplate. Dennis EDITED: Added additional information, and replied to question. An interesting way to make

How to add item to the beginning of the list in ListBox?

北城余情 提交于 2019-12-04 09:59:06
问题 Is there a way to add item to a WinForms ListBox, to the beginning of the list without rewriting entire list in a loop? Other way to solve my problem would be to display ListBox in reverse order (last item on the top) but I don't know how to do it. My ListBox control is used as a log viewer where the most recent entry should be on the top. 回答1: Use the Insert method on the items of your ListBox . 回答2: If I understand correctly, can't you use the Insert(int index, object item) method? For