So this one has been driving me a bit batty...
I have a ListView objected which I've created by dragging and dropping from the toolbox onto the graphic window designer area. I've given it a name like "myListView" in the properties panel, and it has worked as expected up until this point (data is being displayed properly, I can graphically select an item and it highlights, etc.)
So today, I am trying to programatically select one of the items in myListView, with the following line of code:
myListView.Items[0].Selected = true;
However, this is giving me a compilation error, specifically that "Selected" is undefined in the object.
Essentially, there is no "Selected" property in myListview.Items[] and I can't figure out why or how to fix it. In fact, there don't appear to be "any" properties at all. Everything I've found on-line seems to confirm that I'm doing this correctly, but, like Alderan, it's just not there. :)
Any ideas?
If you are using WPF, ListView.Items is a collection of objects, hence you don't see any properties. To select the first item in a WPF ListView, you can do this:
myListView.SelectedIndex = 0;
Related
I was trying to get the text that I wrote in a DataGrid cell after editing it, so I put a breakpoint in the function CellEditEnding and looked at the EventArgs and noticed that it contains the property "Text", so I wouldn't have to do the usual XAML binding hacks to get it.
However, I quickly noticed that it will not let me access it.
After taking a look at the FrameworkElement class, I can confirm that there is no Text property, so what is going on, why can't I acces the property?
why can't I acces the property?
Because a FrameworkElement indeed has no Text property.
TextBox, which derives from FrameworkElement, has a Text property though so you could cast the EditingElement to a TextBox and then access the property:
string text = (e.EditingElement as TextBox)?.Text;
Visual Studio displays the properties of the actual object in memory.
It's a bad idea to use the UI as a data store and try and directly work with it.
You should bind an observablecollection of t to the itemssource of your datagrid and work with each instance of t.
That will be far easier to work with.
As to why are some properties inaccessible?
It's because those properties aren't where you think they are. The DatagridCell has a series of things nested within it.
DataGridCell > Border > ContentPresenter > TextBlock
Download snoop https://github.com/snoopwpf/snoopwpf or install using chocolatey / your preferred method.
Run snoop.
Run your app.
Drag the right gunsight thing over you window.
A window should open up with two panels. Controls and properties.
Mouse over a datagrid cell.
press shift+ctrl and you should see the element under the mouse selected in the ui tree.
A datagrid is pretty complicated and there are multiple things in each row.
See that textblock there?
That's the thing has a text property.
Or at least that's the thing when you're not in edit mode.
Switch to edit mode and I have a TextBoxView.
So one complication is, which are you working with at a given time?
I am using CodedUI technology to do the automation.I have a combo box control and a grid inside as an item. I can find the combo box and managed to open the grid as shown in Fig (01) Control with properties. Now,for further testing I need to click on row item inside the grid.
Issue : I couldn't find the grid and failed to navigate using the search properties. As you see in Fig (01) , the parent level is in desktop client and not bound to our application. Even though the grid belongs to the combo box visually, still it is not listed either parent or the application window.
Solution required: I need to select/click on row item in the grid using hand coded coded ui.
Code Snippet
Note : Playback control also failed to do the desire output.Clicking on the row based on mouse co-ordinates is not much appreciated.
Looking for the solution and thanks much in advance.
Try identifying the grid/Table with combobox as a parent instead of your main application.Something like this
if cboName is your combobox then
HtmlTable tbldata = new HtmlTable(cboName);
Provide any of the columnheaders or one of the Search Property to identify it.
Hope it helps
Hope you must have tried highlighting the grid and checked the parent.
Just try to do record and play u r case and generate code .And see code is getting generated ,how it is maintaing child parent relationship.Than u will get some idea and parameterized the thing in your code
I want to do drag and drop between 2 listboxes, but be able to select multiple items in one listbox, and drag those across to the other listbox. I need to make sure I don't accidentally "drop" them back on the source listbox. I did this in Borland Builder C++ successfully, but can only drag and drop one item at a time using the C# methods. I have gone through all the possibilities here, but none of them gives me a nudge in the right direction. In BCB the DragDrop method had references to the Sender and Source objects, which you could use to determine where the item came from. You could then iterate through the selected list and move them across from source to destination as needed. Is there anything similar available in C# using windows forms, not WPF?
I hope I make some sense in this question!
Set the listbox property SelectionMode to SelectionMode.MultiExtended. To be able to select multiple items.
listBox.SelectionMode = SelectionMode.MultiExtended;
My code is looping through operations and is adding feedback to a listview control. Previously, my code has been working OK, but something has changed today...
The listview control starts off like this:
...but after I issue a call to Update() or Refresh(), rather than show the items that I have added, it renders like this:
At this point, the control is still enabled and visible. In fact the only interaction my code has with it is to add new items and Update().
To add items, I'm using the following:
lvwDrawings.Items.Add(new ListViewItem(new string[]
{
drawing.PartNo,
drawing.Revision.ToString(),
drawing.Issue,
drawing.DrgTypeText,
errorStatus !=null ? errorStatus : drawing.Status,
drawing.Filepath
}));
In case I'd introduced some problem here, I tested with the simpler lvwDrawings.Items.Add("test");, but the result was the same. If I do a QuickWatch, the control correctly tells me that it contains x items...
OK, I've found the problem - for some unknown reason, where I'd previously had lvwDrawings.Items.Clear() to clear the list between different runs, I somehow ended up with lvwDrawings.Clear(). This clears not only the items in the listview, but also the columns.
It's curious on two counts: I would have though that when I tried to add a ListViewItem with specific columns that it would have objected when there weren't any columns, and also, what on earth did I think I was doing when I made the change(??!).
Moderately interesting diversion:
I discovered the problem by creating a second listview below the first, and working through until I hit the problem. As part of that process, I added columns in the design, one of which I called Path, to which the designer didn't object.
However, in the code, references to methods of the static class Path (e.g. Path.GetDirectoryName()) resulted in an error - 'ColumnHeader Path does not have a method 'xxx'' or similar. Clearly, it assumed that when I referred to Path, I was referring to the column within the listview.
Furthermore, when I went to rename the column (to 'FullPath'), it renamed all references to Path in the code, eg. FullPath.GetDirectoryName()....
I would like to have the standard silverlightcombobox behave like an html combobox.
So let's say I have a combobox for all states in the US, If I press the 'I' key, it should navigate the selected item to start at the I's ... Is there anyway to do this, it doesn't make sense that it's not built in functionality.
Maybe I missed the memo? Any ideas?
It's not something that comes as standard - even in Silverlight 4 (I hit this very problem today).
However, there are quite a few DIY implementations on the net:
http://gistom.blogspot.com/2009/12/silverlight-combobox-with-keyboard.html
http://www.codeproject.com/KB/silverlight/ComboBoxKeyBrdSelection.aspx
http://www.reflectionit.nl/Blog/PermaLinkd137c1f7-a515-4084-8199-f8b3cf892b8f.aspx
The author of the last post
created a small Behavior which fixes this problem. You can attach the KeyboardSelectionBehavior to a ListBox or ComboBox using Microsoft Expression Blend. You drag it from the Assets and drop it on your ComboBox or ListBox. If you have a custom ItemTemplate you will have to set the SelectionMemberPath property.
If you don't have access to Blend then just use the code as a template and edit the XAML by hand to produce the same result.