I'm trying to accomplish what should be a very simple task using the ListView Control. All I am trying to do is add two Items to a ListView Control. One Item goes under the "Title" Column and the other Item goes under the "Status" Column. I have seen many examples for the ListView Control and none of them cover this particular need.
I've read the MSDN documentation on the ListView Control and find it rather odd they don't mention this... Or maybe I've overlooked it?
In ListView parlance, these are not separate items. It sounds like you want to add one item, and then what ListView calls a subitem. Assuming that Title is your first column and Status your second, you want:
ListViewItem myItem = listView.Items.Add("My Item's Title");
myItem.SubItems.Add("My Item's Status");
If you are just getting started with a ListView, you can save yourself a lot of time, headaches and frustration by using ObjectListView -- an open source wrapper around .NET WinForms ListView.
It solves many problems that you are going to find while trying to use a ListView, as well as just making it much easier to use. Just one example: it automatically handles sorting rows by clicking on column headers.
Related
I have a very complex databinding I want to accomplish here using the below:
2 SQL CE tables named mainTable and secondaryTable
1 Fluidkit ElementFlow control named myElmntFlow
2 UserControls named myUsrCtrl and otherUsrCtrl
All of the above are already created and implemented but the UserControls are populated into the myElmntFlow control's items list programmatically through lenghty backgroundworker code that does take a good bit of time to run when the number of items to enter is > 20.
This is how they get created as of now:
The backgroundworker loops through each row of mainTable and adds the myUsrCtrl control to the list of items in myElmntFlow if the value of the row in column "Selected" = "'Yes'".
Then, it modifies the content of the newly added myUsrCtrl as such: it adds a otherUsrCtrl into the myUsrCtrl's stackpanel (named stckPanel) for each row in secondaryTable where the value of the column "FullName " = the value of the same column of the mainTable row we used to created the myUsrCtrl control.
And then populates the otherUsrCtrl's sevaral labels with the value of the secondaryTable row looked at at the moment.
Very confusing but it is a complex scenario. Let's use an example:
In mainTable, the row #4 has the FullName value of "Chad Jones" and
also has the Selected value of "Yes".
A new instance of the myUsrCtrl control is added to the
myElmntFlow's list of items as such:
myElmntFlow.Items.Add(myUsrCtrl);
The newly added myUsrCtrl control has a stackpanel (stckPanel)
We filter the secondaryTable where the FullName = "Chad Jones"
For each row in the now filtered secondaryTable, we add a new
instance of otherUsrCtrl to the previously created myUsrCtrl's
stckPanel control
The different labels in the otherUsrCtrl are populated with the values of the
row in secondaryTable
Can this possibly be converted into a DataBinding within the XAML of the controls as I want to implement several features later on (such as a nice SearchBox with autocomplete) that would be quite poor if they were to be coded behind by writing hundreds of line to tell which data to filter, sort , take , compare etc...
I wrote this as clearly as I could, just hope it's understandable.
PS: I would like to keep my SQL structure as the data is going to become quite consequent over time and I believe that the SQL is the way to go when manipulating thousands of lines.
It's not a very confusing scenario, it's just made confusing by the complex handling that goes on there. It can indeed be made much easier using bindings and MVVM (Model-View-ViewModel) so please take some time to read about the basics of that. There are a ton of tutorials and introductory materials on the web, a simple search will give you more than enough to go on.
When you're comfortable with the concepts, all you need is to transform the data into a sequence of objects (no matter how you go about it), then use an ItemsControl to represent the UI for a list of items. Use DataTemplates to specify how each item should be displayed, binding elements in the DataTemplate to the properties of each item. These things can be nested so you can have ItemsControls in your DataTemplates which use other DataTemplates etc.
In order to represent a collection of items bound to an ItemsControl look at using an ICollectionView which will help tremendously with filtering/sorting/etc.
Sorry about the very broad strokes but it is a pretty broad question. If you need more specific help I'll gladly add more.
Being new to WPF I am looking for guidance on which control would be best to use for the following scenario.
I need to create an application which displays two lists side by side. The list on the left displays a list of users. The list on the right displays a list of permissions.
I want to be able to dynamically add/remove a column of check boxes indicating the user permissions to the right hand permission list by clicking/dragging users from the left hand list. The number of user permission columns will be from 0 - total number of users. The check box should bind to the underlying data i.e. update the users permission.
Which control should I use to achieve the right hand permission list, a ListView or DataGrid?
I have something working by using a ListView and creating an attached property using the following answer as a guide WPF MVVM: how to bind GridViewColumn to ViewModel-Collection? but this doesn't render a checkbox only true/false for the permission status. Looking into how to dynamically add a CellDataTemplate to render a checkbox has made me question whether I should be using a DataGrid.
If I was to use a DataGrid how would the dynamic addition of columns work?
I would like to keep the code behind to a minimum if at all possible.
At first glance on your question, I thought a Datagrid would be a better choice. I'm a vb.net guy but I think the syntax would be very similar
To add columns at runtime the code behind looks like this
DataGridView1.Columns.Add("Column Name", "Column Heading")
You could always just do a google search on "Adding columns to a DataGrid at runtime in C#" and that should return a bunch of examples.
I am new to WPF and c#, and I am trying trying to create an excel like table with a fixed number of columns and a varying number of rows according to the user's needs.People advise to use a datagrid, but I am very confused, some say that it is used mostly to display data source content (which is data contained in database if I understood). I read also about listviews, binding things to itemsource...etc. This is really a lot of information to work with! What I simply need is a way to create a table with fixed columns, and adding rows automatically when the user clicks on a button, that's it! No binding, or anything of this sort.But how to achieve that? Also, if you have good websites tutorials for working with datagrids, I would be very grateful (most of those that I found are too much complex, or don't explain well).
Thanks a lot!
You can create datatemplates that will style your data to look however you like - in this case an Excel row. Then you can display this data as the ItemsSource in an items control. Since you want the number of rows to vary based on some criteria, your data should be in an ObservableCollection. I'm not sure how you'd set-up the header, but I think you could style-up some containers and bind their width properties to the datatemplate controls.
If you wish to do more cell level customizations, then you can try Grid
http://www.syncfusion.com/products/user-interface-edition/wpf/grid/grid-control
How to this for combobox?I have long stackpanel which first it has only combobox.And when i select anything from combobox it will be added as button or other thing like on the picture.And it can be closed
[EDIT] I need anything on WPF
This should do the job nicely in WPF; http://www.codeproject.com/KB/WPF/MultiComboBox.aspx
The combo box that ships with WPF does not support selecting multiple
items...
This article describes a WPF combo box that supports both. It also
describes a list box, which the combo box inherits from, that supports
binding to the SelectedItems
Alternatively: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)
Recently I’ve come across something weird… I needed a ComboBox that will allow the user to select multiple items.
The the solution coming to mind is using CheckBoxes. I have found several examples, but neither one displayed the selected items with pretty commas like this;
i m not sure what you are asking for but you can use this.
jquery tokenizing
what about this?
jQuery Chosen Plugin
http://davidwalsh.name/dw-content/jquery-chosen.php
Within my winform(C#) I have listView with two columns, which is used to display the results from searching an array, which is the basic details of an array entry(All data within the array is type string). I am able to add and remove items to the listview, but I would like to be able to gain the value of the row that has been clicked within the listview.
I am planning to use this to update the display full entry info, but I cannot gain the value of the selected item. I was reading elsewhere and saw people mentioning the .SelectedIndex property, but this property is not available to me when I am trying to code.
I could use two listboxes instead of a listview, but the listview is much neater. Also, I am a sofware design (high school) student, and have been learning C# for one and a half years. I am competent at programming, but I start to get lost when things start to get very complex.
Can anybody help?
The ListView.SelectedItems property works fine if the ListView is not in VirtualData mode.
you should use the SelectedItem property and Tag property
var item = (MyClass) listView1.SelectedItems[0].Tag;
tag property allow you to set any type for example MyClass
when you populate the ListView set the Tag property.
For ListView check SelectedItems
You could trap the SelectedIndexChanged event and in that you could do
ListView.SelectedListViewItemCollection listItems=
this.myListView.SelectedItems;
foreach ( ListViewItem item in listItems)
{
MessageBox.Show(item.SubItems[0].Text);
MessageBox.Show(item.SubItems[1].Text);
}
I have fixed the problem. After talking to a friend in my software class, he told me to use listView1.FocusedItem.Index, which works perfectly for my needs, as it gains the index of the selected item. Also, thank you to everyone for their input in trying to help me.