I'd like to make checkboxlistview in wpf application like this image
)
If I check any item on left listview the items of right one should be checked.
I can't use binding, because It's shared project and I need to add UWP project after finish WPF project.
So I can't make binding items to VeiwModel for this project.
I'd like to catch the click event of left items and set the check property to right items. But I can get the checkbox objects from the right listview.
void OnCheckboxClicked1(object sender, RoutedEventArgs e)
{
var checkBox = sender as CheckBox;
var id = checkBox.Tag;
CheckSubList(checkBox.IsChecked, id.ToString());
}
void OnCheckboxClicked2(object sender, RoutedEventArgs e)
{
}
void CheckSubList(bool? isChecked, string Id)
{
foreach (object listView in LogicalTreeHelper.GetChildren(rightListView as FrameworkElement))
{
Console.WriteLine("OKOK");
//foreach (object checkbox in LogicalTreeHelper.GetChildren(obj as FrameworkElement))
//{
// // Some code
//}
}
}
If anyone have a good idea, please help me. Thanks
Related
I have many List Views in an UWP application and I would want to be able to move one item from one List View to another List View
I know about the AllowDrop or CanDragItems properties and that you need to handle some events for drag and drop to work, although I just don't know how to do it.
If you want to add ListView controls by clicking Add button and move items between ListView controls, please check the following code as a sample. The following code is different from the offical sample, it use ObservableCollection to complete the drag and drop operation. You could drag an item from source ListView item to target ListView, and also drag an item from original target ListView to original source ListView.
You could click the Add button twice and then two ListView with two items are added. You can drag any item from any ListView control to another. If you want to keep the dragged item in the source ListView control, just comment the code dragCollection.Remove(dragedItem as string); .
For example:
private ObservableCollection<string> dragCollection;
private ObservableCollection<string> dropCollection;
private object dragedItem;
private ListView dragListView;
private ListView dropListView;
……
private void AddButton_Click(object sender, RoutedEventArgs e)
{
ListView listView = new ListView();
listView.CanDragItems = true;
listView.CanDrag = true;
listView.AllowDrop = true;
listView.ReorderMode = ListViewReorderMode.Enabled;
listView.CanReorderItems = true;
listView.ItemsSource = new ObservableCollection<string>() { "item1","item2" };
listView.DragItemsStarting += ListView_DragItemsStarting;
//listView.DropCompleted += ListView_DropCompleted;
listView.DragEnter += ListView_DragEnter;
listView.Drop += ListView_Drop;
listView.DragOver += ListView_DragOver;
listView.BorderBrush = new SolidColorBrush(Colors.Red);
listView.BorderThickness = new Thickness(1);
stackPanel.Children.Add(listView);
}
private void ListView_DragOver(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Move;
}
private void ListView_Drop(object sender, DragEventArgs e)
{
dropListView = sender as ListView;
if(dropListView!=null)
{
dropCollection = dropListView.ItemsSource as ObservableCollection<string>;
if (dragedItem != null)
{
dropCollection.Add(dragedItem as string);
//If you need to delete the draged item in the source ListView, then use the following code
dragCollection.Remove(dragedItem as string);
dragedItem = null;
}
}
}
private void ListView_DragEnter(object sender, DragEventArgs e)
{
e.AcceptedOperation = (e.DataView.Contains(StandardDataFormats.Text) ? DataPackageOperation.Move : DataPackageOperation.None);
}
private void ListView_DropCompleted(UIElement sender, DropCompletedEventArgs args)
{
var listView = sender as ListView;
if (listView != null)
{
dropListView = listView;
dropCollection = listView.ItemsSource as ObservableCollection<string>;
if(dropListView==dragListView)
{
return;
}
}
}
private void ListView_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
{
var listView = sender as ListView;
if(listView!=null)
{
dragListView = listView;
dragCollection = listView.ItemsSource as ObservableCollection<string>;
if (dropListView == dragListView)
{
return;
}
if(e.Items.Count==1)
{
dragedItem = e.Items[0];
e.Data.RequestedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
}
}
}
For more information about dragging and dropping, you could refer to the document(https://learn.microsoft.com/en-us/windows/uwp/design/input/drag-and-drop).
Any concerns about the code, please feel free to contact me.
To implement dragging, you must set CanDragItems on the source ListView and AllowDrop on the target ListView. Then, you must handle DragItemsStarting event on the source list. Within this handler you can store the dragged data inside the DragItemsStartingEventArgs.Data property. Afterwards, you handle Drop event on the target list and retrieve the stored item values from the DataPackage using DragEventArgs.DataView.
To see all the moving parts of this in action, I recommend the official UWP samples for drag & drop which are available on GitHub. The first scenario of this sample show dragging items from and to a ListView including reordering support.
I create a listview called listBoxobj. I want to delete the listview based on the selected listview but right now the problem is that when deleting it will delete by index not the selected item.
This is for an apps develops in visual studio,running SQLite.
i've already tried to use this code but keep failed
foreach ( ListViewItem eachItem in listBoxobj.SelectedItems)
{
listBoxobj.Items.Remove(eachItem);
}
I want the selected listview will be deleted instead the listview deleted by index
private void DeleteMV_Click(object sender, RoutedEventArgs e)
{
DatabaseHelperMV_Meriam delete = new DatabaseHelperMV_Meriam();
x = listBoxobj.SelectedItems;//this line keep getting error
delete.DeleteId(x);
Frame.Navigate(typeof(MV_MeriamViewData));
}
For WinForms:
You delete list view selected item using this:
listBoxobj.SelectedItems[0].Remove();
This code you have:
foreach ( ListViewItem eachItem in listBoxobj.SelectedItems)
{
listBoxobj.Items.Remove(eachItem);
}
Is corrected if you have multiselect = true... but if you have multiselect = false, use this: listBoxobj.SelectedItems[0].Remove();
For WPF:
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selected = listBoxobj.SelectedItems.Cast<Object>().ToArray();
foreach (var item in selected)
{
listBoxobj.Items.Remove(item);
}
}
Hy!
I would like to create an ObjectListView, where you can delete items with a ContextMenu.
So basicly I used to delete it by getting OLV.SelectedIndex, and then deleting from the list OLV is based on, and re-setting the OLV objects.
Then I realized, if I sort the OLV, then delete an item, it deletes another item, since the selected item index does not equals the index in the list.
With OLV CellRightClick event I can get the object behind the clicked item (e.Model),but I dont know how to pass it to the ContextMenu click event handler.
Subjects is a List.
private void subjectListView_CellRightClick(object sender, BrightIdeasSoftware.CellRightClickEventArgs e)
{
if (subjectsListView.SelectedIndex != -1)
{
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("Delete", new EventHandler(DeleteItem));
subjectsListView.ContextMenu = cm;
}
}
void DeleteItem(object sender, EventArgs e)
{
//get the Subject object, which was clicked on
Subjects.RemoveAt(subjectsListView.SelectedIndex);
subjectsListView.SetObjects(Subjects);
}
So basically I want to get the object (not the index) when the ContextMenus "Delete" item is clicked.
Also, I feel like there is an easier way to do this.
Thanks for the answer.
I would just assign an appropriate ContextMenuStrip from the designer to the ObjectListView.ContextMenuStrip property and then handle the click of the corresponding "Delete" click like this:
private void deleteToolStripMenuItem_Click(object sender, EventArgs e) {
if (objectListView1.SelectedObject != null) {
objectListView1.RemoveObject(objectListView1.SelectedObject);
}
}
Or is there a requirement I am missing from your question?
I want to be able:
to open a mail when the user taps on one item.
and delete multiple emails when the user selects multiple emails
So I choosed LongListMultiSelector.
In built in LongListSelector, I handle the SelectionChanged event like this:
private void mails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedItem = mailsLongListSelector.SelectedItem as Mail;
if (selectedItem == null)
return;
...
mailsLongListSelector.SelectedItem = null;
}
I want exactly like that functionality in wptoolkit's LongListMultiSelector. like when you select an email to open and read it.
LongListMultiSelector's SelectionChanged occurs when you tap left side of an item and checkboxes appear. this is not what I want.
The Question is:
How can I perform something when the user taps on one item of LongListMultiSelector? thanks.
You could try this. If this is your LongListSelector
<tkit:LongListMultiSelector Name="longlist" SelectionChanged="longlist_SelectionChanged">
<tkit:LongListMultiSelector.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" FontSize="32" Tap="TextBlock_Tap"/>
</DataTemplate>
</tkit:LongListMultiSelector.ItemTemplate>
</tkit:LongListMultiSelector>
and it has an itemtemplate, you can detect a tap on item.
private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var itemTapped = (sender as FrameworkElement).DataContext as Book;
}
and still have a selection changed
private void longlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
Once you are using LongListMultiSelector, the SelectionChanged event is fired when item is added or removed. If you want to perform the action regardless item is added/removed, I've managed to do it like this (for a simple string):
private void longlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selectedItem = String.Empty;
if (e.AddedItems.Count > 0) selectedItem = e.AddedItems[0] as string;
else selectedItem = e.RemovedItems[0] as string;
MessageBox.Show(selectedItem); // do your work
}
It should run while items are selected separately by tapping, but this method will have problems when more items are added/removed at the same time - if you need it, then you should handle this also.
I want to select item in a ListView upon clicking. I also want to know what I clicked.
I work on winforms with c#.I also want to know How I can clicking the all row?
Just handle the Click event on the list and use the ListView.SelectedItems property to get what items are selected:
private void listView1_Click(object sender, EventArgs e)
{
var firstSelectedItem = listView1.SelectedItems[0];
}
u can use MouseEventArgs and get the mouse location check if it exists inside the selected item bound , that means the click was made on the selected item .
EDIT :
example :
private void myList_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (myList.SelectedItems.Count >= 1)
{
ListViewItem item = myList.SelectedItems[0];
//here i check for the Mouse pointer location on click if its contained
// in the actual selected item's bounds or not .
// cuz i ran into a problem with the ui once because of that ..
if (item.Bounds.Contains(e.Location))
{
MessageBox.Show("Double Clicked on :"+item.Text);
}
}
}
also if you use xaml for window then you must add MouseUp="listView1_Click" attribute to ListView tag