1
votes

My DataGrid's SelectedItem is bound to the property below.

public OrderItemViewModel SelectedItem
{
    get { return _selectedItem; }
    set
    {
        if (_selectedItem != value)
        {
            _selectedItem = value;
            OnPropertyChanged(() => SelectedItem);

            if (_selectedItem != null && _isReturnMode)
            {
                if (_selectedItem.OrderItemModel.ProductDetails.IsConstructed)
                {
                    VisiblePaymentViewModel = new ViewReturnComponentsViewModel(this, value.OrderItemModel.ProductDetails);
                }
                else
                {
                    VisiblePaymentViewModel = new EditReturnItemViewModel(this, value);
                }
                SelectedItem = null;
            } 
        }
    }
}

The DataGrid's ItemsSource is bound to

public ICollectionView VisibleOrderItems { get; set; }

The reason why I want to reset the SelectedItem in the program, is because this is a touch screen application, and when they select the item, I want it to change the View, then when when they get back to the View with the datagrid I do not want an item to be selected.

I've tried creating a separate function that just clears out the SelectedItem, and calling that when the separate View is finished, and also tried variuous binding settings.... UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, IsSynchronizedWithCurrentItem

Can't wrap my head around why this doesn't work.

The DataGrid shows the row as highlighted, but when I click on that row it sets _selectedItem (_selectedItem != value)

3
are doing any null checks in "OnPropertyChanged" method? if possible please provide the code in the OnPropertyChanged method.Bathineni

3 Answers

1
votes

Have you tried setting VisibleOrderItems.CurrentItem = null?

Also, there is a difference between current items and selected items, with current being the one occupying the CurrentItem property in your ICollectionView, and SelectedItems being the set of all items currently selected by whichever means. The current item need not be selected.

0
votes
if (_selectedItem != value)
        {
            _selectedItem = value;

            if (_selectedItem != null && _isReturnMode)
            {
                if (_selectedItem.OrderItemModel.ProductDetails.IsConstructed)
                {
                    VisiblePaymentViewModel = new ViewReturnComponentsViewModel(this, value.OrderItemModel.ProductDetails);
                }
                else
                {
                    VisiblePaymentViewModel = new EditReturnItemViewModel(this, value);
                }
                SelectedItem = null;
            } 
             OnPropertyChanged(() => SelectedItem);
        }

Set Notify at the end of if block.

0
votes

In one my project I have exactly the same problem as you and I did the very hackish solution bellow. In xml - NotifyOnSourceUpdated=True is important!:

      <DataGrid ItemsSource="{Binding Items}" 
SelectedItem="{Binding SelectedItem, Mode=TwoWay, NotifyOnSourceUpdated=True}">

In viewmodel property should look like code bellow. Note that code does not contains your if conditions, but im sure its clear enough:

    public TestModel SelectedItem
    {
        get { return selectedItem; }
        set
        {
            selectedItem = value;

            if (selectedItem != null)
            {
                //.....do something with selected item

                //actual hack, execute SelectedItem = null async on UI thread
System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke((ThreadStart)(() =>
                 {
                     SelectedItem = null;
                 }));
             }
             else
             {
                 OnPropertyChanged("SelectedItem");
             }               
        }
    }