I want to automatically put the card in edit mode when the user adds a new card

1 Answer 22 Views
CardView
HemoSonics
Top achievements
Rank 1
Iron
Iron
HemoSonics asked on 30 Nov 2023, 08:56 PM | edited on 01 Dec 2023, 07:26 PM

I don't see an easy way to do this.  I Selected the added item in the view model and then sent a message to the view to get it to call the Begin Edit method, but that didn't cause the card to go into edit mode.

Even though I added an item to the list that was bound to the Items source of the RadCardView,  I noticed when I made the below call (from the view code behind), the list of cardViewItems did not contain the newly added object that I added to the source.  Not sure why it is not automatically updating the cardviewitems, as the itemsSource is bound to an Observable Collection.  

  IEnumerable<RadCardViewItem> cardViewItems = cardView.ChildrenOfType<RadCardViewItem>();

 

I think that when BeginEdit is called, it is not finding the CardViewItem and therefore it does nothing.  Not sure how to fix that.   Maybe there is a bug in the cardview class.

HemoSonics
Top achievements
Rank 1
Iron
Iron
commented on 30 Nov 2023, 08:58 PM

Here is some of my code:  

 public InstrumentTopographyControl()
        {
            InitializeComponent();
            InstrumentListAddMessage.Register(this, HandleInstrumentListAdd);
        }

        private void HandleInstrumentListAdd(InstrumentListAddMessage obj)
        {
            OnCardBeginEdit(null, null);
        }

        private void OnCardBeginEdit(object sender, MouseButtonEventArgs e)
        {
            // start editing 
            cardView.PendingCommands.Add(Telerik.Windows.Controls.RadCardViewCommands.BeginEdit);
            cardView.ExecutePendingCommand();
        }

1 Answer, 1 is accepted

Sort by
0
Dinko
Telerik team
answered on 04 Dec 2023, 02:52 PM

Hello,

To achieve this requirement and set the newly added item immediately into edit mode you could use Dispatcher.BeginInvoke method. In this method you could use the customized command BeginEdit. Here is an example how this Dispatcher.BeginInvoke method should looks like:

this.Dispatcher.BeginInvoke(new Action(() =>
{
	this.cardView.PendingCommands.Add(RadCardViewCommands.BeginEdit);
	this.cardView.ExecutePendingCommand();
}), System.Windows.Threading.DispatcherPriority.Loaded);

The new RadCardViewItem is not immediately found right after adding a new item to the ItemsSource collection, because after adding an item, WPF requires at least one more layout pass to generate the visual element (RadCardViewItem). Therefore, a brief pause is necessary to allow for this process, and this is where the Dispatcher comes into play, ensuring the correct timing for the UI update.

For your convenience I prepared a sample solution where this logic is implemented.

Regards,
Dinko
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

HemoSonics
Top achievements
Rank 1
Iron
Iron
commented on 04 Dec 2023, 06:32 PM

Thanks.  This works perfectly!
Tags
CardView
Asked by
HemoSonics
Top achievements
Rank 1
Iron
Iron
Answers by
Dinko
Telerik team
Share this question
or