Reputation: 480
I'm trying to get my ListView to contain one column that is a ComboBox. I'm adding multiple entries to the list on startup. One of the columns is "Versions". There can be multiple versions and I want to give the user the opportunity to choose which version of the item he/she wants to update to.
Current ListView :
lv_requirements.CheckBoxes = true;
lv_requirements.View = View.Details;
//add columns to list view
lv_requirements.Columns.Add("Name", 270);
lv_requirements.Columns.Add("Type", 150);
lv_requirements.Columns.Add("Status", 100);
lv_requirements.Columns.Add("Current Version", 80);
lv_requirements.Columns.Add("Latest Version", 80);
And I add items to list as follows:
String[] itemContent = new String[5];
itemContent[0] = Name;
itemContent[1] = Type;
itemContent[2] = ID;
itemContent[3] = CurrentVersionNumber;
itemContent[4] = LatestVersionNumber;
ListViewItem add = new ListViewItem(itemContent);
add.Tag = handle;
//Add image to list item
add.ImageIndex = item.DisplayImageIndex;
lv_requirements.SmallImageList = icons;
lv_requirements.Items.Add(add);
What I would like to modify is:
object[] itemContent = new object[5];
itemContent[4] = new ComboBox([AllLaterVersions]);
But as I understand it there is no way to initiate a new ListViewItem with anything but Strings. Is there someway for me to either have a ComboBox with all versions or if not possible, at least have an inputable text field in that column instead where the user can input the version of his/her desire.
Best Regards
Upvotes: 0
Views: 10582
Reputation: 194
you could create an entity ViewModel with the values you need + 2 bool propreties that represent the user choice.
Then you can easily add DataTemplate to your ListView.
The DataTemplate will bind the checkbox (but i think using radio-buttons will be better choice here) to the properties of the VM.
<ListView>
<ListView.View >
<GridView >
<!-- other columns you need-->
<GridViewColumn Header="Versions" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsCheck="{Binding LatestVersion}"/>
<CheckBox IsCheck="{Binding CurrentVersion}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Upvotes: 0
Reputation: 3138
Your understanding is correct, ListView by design is intended for String Literals as its item. There might be some hack to actually do something similar as you want but I am pretty certain it won't be a very elegant solution.
So my advice, dump ListView and use DataGridView, with very little effort you can make the GridView look quite similar to ListView and you can add a dropdown and what not...
Upvotes: 1