wpf Update TreeView checkboxes during loop - wpf

I'm starting out in wpf.
I have a TreeView in which each item has a checkbox.
I'm trying to create an animation in which The checkboxes are checked programmatically inside a loop.
After researching the topic for some time, I came up with the following method -
private void Traverse_Click(object sender, RoutedEventArgs e)
{
ItemCollection items = tvMain.Items;
Task.Factory.StartNew( ()=>
Dispatcher.Invoke( (Action)(() =>
{
foreach (TreeViewItem item in items)
{
UIElement elemnt = getCheckbox();
if (elemnt != null)
{
CheckBox chk = (CheckBox)elemnt;
chk.IsChecked = !chk.IsChecked;
tvMain.Items.Refresh();
tvMain.UpdateLayout();
Thread.Sleep(500);
}
}
})));
And yet despite all of my attempts the the tree doesn't update inside the loop, only at the end. so the checkboxes are all checked at once.
How can I make the tree update inside the loop?
Thanks

Replace Thread.Sleep with Task.Delay to "sleep" asynchronously:
private async void Traverse_Click(object sender, RoutedEventArgs e)
{
ItemCollection items = tvMain.Items;
foreach (TreeViewItem item in items)
{
UIElement elemnt = getCheckbox();
if (elemnt != null)
{
CheckBox chk = (CheckBox)elemnt;
chk.IsChecked = !chk.IsChecked;
await Task.Delay(500);
}
}
}

Related

How to get the Logical Children of a RowDefinition?

I have Extended the RowDefinition as RowDefinitionExtended and In that, when can i get the LogicalChildren belongs to this RowDefinition. I mean in which override can i get the LogicalChildren?
public class RowDefinitionExtended : RowDefinition
{
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
Loaded += OnRowDefinitionExtendedLoaded;
}
void OnRowDefinitionExtendedLoaded(object sender, RoutedEventArgs e)
{
var parent = GetUIParentCore() as Grid;
if (parent == null) return;
if (parent.Children.Cast<UIElement>().Where(c => Grid.GetRow(c) == parent.RowDefinitions.IndexOf(this)).All(ctrl => ctrl.Visibility != Visibility.Visible))
Height = new GridLength(0);
}
}
What my requirement is, I need to check all the LogicalChildren to its Visibility and Change its Height accordingly.
How could i do this? Any idea?
Update:
Code has been updated, On Load I could do this and it works fine. But my problem is, am changing the controls visibility after load... So is there any notification while changing the Visibility? am looking a event when the layout updated like..
Any event can i use it for?
You can't do that by means of a derived RowDefinition, but this little helper method should do the job (if your intention was to get all child elements in a certain row of a Grid):
public static IEnumerable<UIElement> ChildrenInRow(Grid grid, int row)
{
return grid.Children.Cast<UIElement>().Where(c => Grid.GetRow(c) == row);
}
You have to subscribe to the IsVisibleChanged handler for each element in the row when the row is loaded.
When the visibility changed, you could do whatever you need
public class RowDefinitionExtended : RowDefinition
{
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
Loaded += OnRowDefinitionExtendedLoaded;
}
void OnRowDefinitionExtendedLoaded(object sender, RoutedEventArgs e)
{
var parent = GetUIParentCore() as Grid;
if (parent == null) return;
//Subscribe to the IsVisibleChanged handler for each element in the row
var ElementInGridRow = parent.Children.Cast<UIElement>().Where(c => Grid.GetRow(c) == parent.RowDefinitions.IndexOf(this));
foreach (var element in ElementInGridRow)
{
element.IsVisibleChanged+=new DependencyPropertyChangedEventHandler(OnChildrenIsVisibleChanged);
}
}
private void OnChildrenIsVisibleChanged(object sender,DependencyPropertyChangedEventArgs e)
{
UIElement element = sender as UIElement;
//Do stuff...
var parent = GetUIParentCore() as Grid;
if (parent.Children.Cast<UIElement>().Where(c => Grid.GetRow(c) == parent.RowDefinitions.IndexOf(this)).All(ctrl => ctrl.Visibility != Visibility.Visible))
Height = new GridLength(0);
}
}

Check only one ToolStripMenuItem

I have a ToolStrip with multiple ToolStripDropDownButtons, each has a set of DropDownItems.
When the user clicks on an DropDownItem, the check mark is shown.
By default, multiple items can be clicked and therefore multiple check marks appear.
What I'm trying to do is when the user clicks one DropDownItem, the other already checked items should be unchecked. In other words, there should always be only one checked item in the DropDown list.
I've been dabbling with it for some time but I can't really figure out how to keep the current checked item as it is while uncheck other items.
Below is the code I have as of now.
private void subietm1ToolStripMenuItem_Click(object sender, EventArgs e)
{
UncheckOtherToolStripMenuItems(sender);
}
public void UncheckOtherToolStripMenuItems(object selectedMenuItem)
{
List<ToolStripDropDownButton> dropdownButtons = new List<ToolStripDropDownButton>();
foreach (ToolStripItem item in toolStrip1.Items)
{
if (item is ToolStripDropDownButton)
{
dropdownButtons.Add((ToolStripDropDownButton)item);
}
}
foreach (ToolStripDropDownButton btn in dropdownButtons)
{
foreach (ToolStripMenuItem d in btn.DropDownItems)
{
if (d.Checked)
d.CheckState = CheckState.Unchecked;
}
}
}
If someone could shed some light on this or tell me an easy way to go about it, I'd be grateful.
Thank you.
So easy...
Implement their method as described below:
private void subietm1ToolStripMenuItem_Click(object sender, EventArgs e)
{
UncheckOtherToolStripMenuItems((ToolStripMenuItem)sender);
}
public void UncheckOtherToolStripMenuItems(ToolStripMenuItem selectedMenuItem)
{
selectedMenuItem.Checked = true;
// Select the other MenuItens from the ParentMenu(OwnerItens) and unchecked this,
// The current Linq Expression verify if the item is a real ToolStripMenuItem
// and if the item is a another ToolStripMenuItem to uncheck this.
foreach (var ltoolStripMenuItem in (from object
item in selectedMenuItem.Owner.Items
let ltoolStripMenuItem = item as ToolStripMenuItem
where ltoolStripMenuItem != null
where !item.Equals(selectedMenuItem)
select ltoolStripMenuItem))
(ltoolStripMenuItem).Checked = false;
// This line is optional, for show the mainMenu after click
selectedMenuItem.Owner.Show();
}
One detail is that you can implement the same method for all click menuItens, for this add same call for method UncheckOtherToolStripMenuItems((ToolStripMenuItem)sender); into the Event click for each ToolstripMenuItem, see this example to the another two ToolstripMenuItens:
private void subietm2ToolStripMenuItem_Click(object sender, EventArgs e)
{
UncheckOtherToolStripMenuItems((ToolStripMenuItem)sender);
}
private void subietm3ToolStripMenuItem_Click(object sender, EventArgs e)
{
UncheckOtherToolStripMenuItems((ToolStripMenuItem)sender);
}
I just set all the items in my menu with the event of item_Click so if one is clicked then it will just run the code below. Dont need an event for each button that way.
private void item_Click(object sender, EventArgs e)
{
// Set the current clicked item to item
ToolStripMenuItem item = sender as ToolStripMenuItem;
// Loop through all items in the subMenu and uncheck them but do check the clicked item
foreach (ToolStripMenuItem tempItemp in (ToolStripMenuItem)item.OwnerItem.DropDownItems)
{
if (tempItemp == item)
tempItemp.Checked = true;
else
tempItemp.Checked = false;
}
}
If you want to add several items to your list during runtime and have them connected in the way above you can run the below code.
private void subItemsMenus(ToolStripMenuItem parentItem, string[] listItems)
{
// Clear tool strip items first
parentItem.DropDownItems.Clear();
// Add items that are in the list
foreach (string subMenuItem in listItems)
{
ToolStripMenuItem item = new ToolStripMenuItem();
//Name that will appear on the menu
item.Text = subMenuItem;
//Put in the Name property whatever necessary to retrieve your data on click event
item.Name = subMenuItem;
//On-Click event
item.Click += new EventHandler(item_Click);
//Add the submenu to the parent menu
parentItem.DropDownItems.Add(item);
}
I have another way which works:
Each item is going to ToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
and every item has its own tag 1, 2, 3, 4, 5.
One item is checked on start and has tag = 1.
int selecteditem = 1;
bool atwork = false;
private void dzienToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
{
if (atwork) return;
else atwork = true;
selecteditem = Convert.ToInt32(((ToolStripMenuItem)sender).Tag);
foreach (ToolStripMenuItem it in sometooltipdropdown.DropDownItems)
{
if (Convert.ToInt32(it.Tag) != selecteditem)
{
it.Checked = false;
}
}
atwork = false;
}
The easiest way is add DropDownItemClicked Event and create own method:
private void toolStripDropDownButton1_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem != null)
{
CheckSelected((ToolStripDropDownButton)sender, e.ClickedItem);
}
}
private void CheckSelected(ToolStripDropDownButton button, ToolStripItem selectedItem)
{
foreach (ToolStripMenuItem item in button.DropDownItems)
{
item.Checked = (item.Name == selectedItem.Name) ? true : false;
}
}
You could get the count by copying to an array and then use extensions.
ToolStripItem[] controls = new ToolStripItem[ToolStrip.DropDownItems.Count];
ToolStrip.DropDownItems.CopyTo(controls,0);
intcheckCount = controls.Count(c => (c as ToolStripMenuItem).Checked);
if (checkCount == 0) // must keep 1 selection
item.Checked = true;
else if (checkCount > 1) //uncheck all others
controls.Cast<ToolStripMenuItem>().Where(c => c.Checked && c.Name != item.Name)
.ToList().ForEach(s => s.Checked = false);

Catch RowEdited event in DataGrid WPF

How to catch event when DataGrid's row was edited and get all values from it?
If I use RowEditEnding event, I can't get new values...
Thanks!
See the discussion here, and this solution:
private void OnRowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;
if (e.EditAction == DataGridEditAction.Commit) {
ListCollectionView view = CollectionViewSource.GetDefaultView(dataGrid.ItemsSource) as ListCollectionView;
if (view.IsAddingNew || view.IsEditingItem) {
this.Dispatcher.BeginInvoke(new DispatcherOperationCallback(param =>
{
// This callback will be called after the CollectionView
// has pushed the changes back to the DataGrid.ItemSource.
// Write code here to save the data to the database.
return null;
}), DispatcherPriority.Background, new object[] { null });
}
}
}

Getting the TreeViewItem on newly created items

There has to be a better way then the following for getting "childItem"
TaskItem task = (sender as Canvas).DataContext as TaskItem;
TaskItem child = Tasks.CreateTask("New task", task);
TreeViewItem item = treeView.ItemContainerGenerator.ContainerFromItem(task) as TreeViewItem;
item.UpdateLayout();
TreeViewItem childItem = null;
foreach (var i in item.GetDescendantContainers())
{
if (i.GetItem() == child)
childItem = i;
}
For some reason item.ItemGenerator.ContainerFromItem(child) does not work (must be due to the item having just been created)
Item container generation is asynchronous, so you cannot assume the container will exist as soon as the item was added. You will need to attach a handler to the ItemContainerGenerator.StatusChanged event so your code will be informed when container generation is complete.
Dr. WPF's blog entry "ItemsControl: 'G' is for Generator" has a good description of the problem and provides an example of using StatusChanged:
private void AddScooby()
{
_scooby = new Character("Scooby Doo");
Characters.Add(_scooby);
CharacterListBox.ItemContainerGenerator.StatusChanged
+= OnStatusChanged;
}
private void OnStatusChanged(object sender, EventArgs e)
{
if (CharacterListBox.ItemContainerGenerator.Status
== GeneratorStatus.ContainersGenerated)
{
CharacterListBox.ItemContainerGenerator.StatusChanged
-= OnStatusChanged;
ListBoxItem lbi = CharacterListBox.ItemContainerGenerator
.ContainerFromItem(_scooby) as ListBoxItem;
if (lbi != null)
{
lbi.IsSelected = true;
}
}
}

What is the "pressed the delete key" event for the WPF Datagrid?

I want to enable the user to highlight a row on the WPF DataGrid and press delete key to delete the row.
the functionality is already built into the UI of the grid, so to the user, the row disappears
I currently handle this on the SelectionChanged event (code below)
I loop through all the "e.RemovedItems" and delete them with LINQ
Problem is: even when you simply select a row and move off of it, selection change is fired and that row is in e.RemovedItems (which is odd, why would simply selecting something put it in a RemovedItems container?).
So I am looking for a DeleteKeyPressed event so I can simply handle it. What is that event called?
I am using the March 2009 toolkit.
XAML:
<Grid DockPanel.Dock="Bottom">
<toolkit:DataGrid x:Name="TheDataGrid"
SelectionChanged="TheDataGrid_SelectionChanged"
AutoGenerateColumns="True"
RowEditEnding="TheDataGrid_RowEditEnding"/>
code-behind:
private void TheDataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count > 0)
{
Message.Text = "The following were removed: ";
foreach (object obj in e.RemovedItems)
{
Customer customer = obj as Customer;
Message.Text += customer.ContactName + ",";
_db.Order_Details.DeleteAllOnSubmit(
customer.Orders.SelectMany(o => o.Order_Details));
_db.Orders.DeleteAllOnSubmit(customer.Orders);
_db.Customers.DeleteOnSubmit(customer);
}
}
try
{
_db.SubmitChanges();
}
catch (Exception ex)
{
Message.Text = ex.Message;
}
}
ANSWER:
Thanks lnferis, that was exactly what I was looking for, here is my finished delete handling event for the datagrid, note the KeyDown event doesn't fire for some reason.
XAML:
<toolkit:DataGrid x:Name="TheDataGrid"
KeyDown="TheDataGrid_KeyDown"
PreviewKeyDown="TheDataGrid_PreviewKeyDown"
AutoGenerateColumns="True"
RowEditEnding="TheDataGrid_RowEditEnding"/>
code-behind
private void TheDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
var grid = (DataGrid)sender;
if (grid.SelectedItems.Count > 0)
{
string checkMessage = "The following will be removed: ";
foreach (var row in grid.SelectedItems)
{
Customer customer = row as Customer;
checkMessage += customer.ContactName + ",";
}
checkMessage = Regex.Replace(checkMessage, ",$", "");
var result = MessageBox.Show(checkMessage, "Delete", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
foreach (var row in grid.SelectedItems)
{
Customer customer = row as Customer;
_db.Order_Details.DeleteAllOnSubmit(
customer.Orders.SelectMany(o => o.Order_Details));
_db.Orders.DeleteAllOnSubmit(customer.Orders);
_db.Customers.DeleteOnSubmit(customer);
}
_db.SubmitChanges();
}
else
{
foreach (var row in grid.SelectedItems)
{
Customer customer = row as Customer;
LoadData();
_db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, customer); //TODO: this doesn't refresh the datagrid like the other instance in this code
}
}
}
}
}
private void TheDataGrid_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("never gets here for some reason");
}
The RemovedItems items reflects the items removed from the selection, and not from the grid.
Handle the PreviewKeyDown event, and use the SelectedItems property to delete the selected rows there:
private void PreviewKeyDownHandler(object sender, KeyEventArgs e) {
var grid = (DataGrid)sender;
if ( Key.Delete == e.Key ) {
foreach (var row in grid.SelectedItems) {
... // perform linq stuff to delete here
}
}
}
XAML
<DataGrid ItemsSource="{Binding}" CommandManager.PreviewCanExecute="Grid_PreviewCanExecute" />
Code behind
private void Grid_PreviewCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
DataGrid grid = (DataGrid)sender;
if (e.Command == DataGrid.DeleteCommand)
{
if (MessageBox.Show(String.Format("Would you like to delete {0}", (grid.SelectedItem as Person).FirstName), "Confirm Delete", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
e.Handled = true;
}
}
What are you binding your DataGrid to?
Ideally, you should react to CollectionChanged events on the collection you are binding to. That way, your logic (deletion of removed items) will be separated from your UI.
You can build an Observable collection containing your objects and bind it to ItemsSource just for that purpose if the original collection does not have the necessary events.
It might not suit your specific setup, but that's how I usually do it.
Please follow the below code. I have succeeded with the below code.
Please let me know if changes are required.
private void grdEmployee_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Device.Target.GetType().Name == "DataGridCell")
{
if (e.Key == Key.Delete)
{
MessageBoxResult res = MessageBox.Show("Are you sure want to delete?", "Confirmation!", MessageBoxButton.YesNo,MessageBoxImage.Question);
e.Handled = (res == MessageBoxResult.No);
}
}
}
A little late to the party, but to get Inferis answer working:
Dim isEditing = False
AddHandler dg.BeginningEdit, Sub() isEditing = True
AddHandler dg.RowEditEnding, Sub() isEditing = False
AddHandler dg.PreviewKeyDown, Sub(obj, ev)
If e.Key = Key.Delete AndAlso Not isEditing Then ...
This fixes epalms comment: "if you're editing a cell and use the delete key to remove some characters in the cell, you'll end up deleting the whole row"
The cleanest solution is to use PreviewCanExecute like answered by flux, this is a completed solution to make it a bit more clear for anybody that overlooked his answer like I did:
private void Grid_PreviewCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (e.Command == DataGrid.DeleteCommand)
{
if (MessageBox.Show($"Delete something from something else?", "Confirm removal of something", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
// Do what ever needs to be done when someone deletes the row
}
else
{
e.Handled = true;
// Handled means.. no worries, I took care of it.. and it will not delete the row
}
}
}
No need to hook on to CommandManager.Executed after this.
You want to handle the KeyUp or KeyDown event and check the pressed Key for Delete.
private void OnKeyDown(object sender, KeyEventArgs e) {
if ( Key.Delete == e.Key ) {
// Delete pressed
}
}

Resources