Retrieve user input from ProWindow dialog without global variables?

494
4
Jump to solution
11-03-2022 03:21 PM
succip
by
New Contributor III

Hello,

I'd like to retrieve user input from a ProWindow dialog, then use that input to do a bunch of stuff in my MapView/Tables. Following this example (https://github.com/Esri/arcgis-pro-sdk-community-samples/tree/master/Framework/ProWindowMVVM), I see them using Module1 to store/retrieve global variables, is this best practice in a case where I'll be building multiple ProWindow forms? I worry it will get overwhelming quickly if I want to track 5-10 properties per window.

Is there any other way of retrieving user input from a ProWindow? The ShowDialog() method only returns a boolean. 

Happy to provide more details but I wanted to keep my question as simple as possible. As of right now my code is as follows as an example:

TestButton.cs:

 

 

namespace SurreyTools
{
    internal class TestButton : Button
    {
        private CreateLotWindow _createlotwindow;

        protected override void OnClick()
        {
            if (_createlotwindow != null)
                return;
            _createlotwindow = new CreateLotWindow();
            _createlotwindow.Owner = FrameworkApplication.Current.MainWindow;
            _createlotwindow.Closed += (o, e) => { _createlotwindow = null; };
            _createlotwindow.ShowDialog();
        }
    }
}

 

 

CreateLotsWindowViewModel:

 

 

namespace SurreyTools
{
    public class CreateLotWindowViewModel: PropertyChangedBase
    {
        private string _lotCount;
        public string LotCount
        {
            get => _lotCount;
            set
            {
                SetProperty(ref _lotCount, value, () => LotCount);
            }
        }

        public ICommand SubmitLotNumber => new RelayCommand((createLotWindow) =>
        {
            (createLotWindow as CreateLotWindow).DialogResult = true;
            (createLotWindow as CreateLotWindow).Close();
        }, () => true);
    }
}

 

 

I'd like some way to pass the lotCount property to either my button or any other use case. 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

On Closed event it destroys objects. I don't use that pattern. You can use local variable like this:

        protected override void OnClick()
        {
            CreateLotWindow createlotwindow = new CreateLotWindow();
            createlotwindow.Owner = FrameworkApplication.Current.MainWindow;
            if(createlotwindow.ShowDialog()== true) {
                var viewModel = createlotWindow.DataContext as CreateLotWindowViewModel;
                var count = viewModel.LotCount;
            }
        }

View solution in original post

4 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

You can use DataContext to access view model properties:

        protected override void OnClick()
        {
            if (_createlotwindow != null)
                return;
            _createlotwindow = new CreateLotWindow();
            _createlotwindow.Owner = FrameworkApplication.Current.MainWindow;
            _createlotwindow.Closed += (o, e) => { _createlotwindow = null; };
            if(_createlotwindow.ShowDialog()== true) {
                var viewModel = _createlotWindow.DataContext as CreateLotWindowViewModel;
                var count = viewModel.LotCount;
            }
        }
succip
by
New Contributor III

Hello, thanks for your help, but I'm getting an error. once code enters the if statement, _createlotwindow becomes null. I assume this is because I'm setting _createlotwindow.Closed to make the object null on close. It works fine if I set the DataContext before the ShowDialog(). Is there any harm in doing that?

protected override void OnClick()
        {
            if (_createlotwindow != null)
                return;
            _createlotwindow = new CreateLotWindow();
            _createlotwindow.Owner = FrameworkApplication.Current.MainWindow;
            var viewModel = _createlotwindow.DataContext as CreateLotWindowViewModel;
            _createlotwindow.Closed += (o, e) => { _createlotwindow = null; };
            if (_createlotwindow.ShowDialog() == true)
            {
                var count = viewModel.LotCount;
            }
        }

PaoloSucci_0-1667576546315.png

 

0 Kudos
GKmieliauskas
Esri Regular Contributor

On Closed event it destroys objects. I don't use that pattern. You can use local variable like this:

        protected override void OnClick()
        {
            CreateLotWindow createlotwindow = new CreateLotWindow();
            createlotwindow.Owner = FrameworkApplication.Current.MainWindow;
            if(createlotwindow.ShowDialog()== true) {
                var viewModel = createlotWindow.DataContext as CreateLotWindowViewModel;
                var count = viewModel.LotCount;
            }
        }
succip
by
New Contributor III

Is there a trick to doing this in a RowCreatedEvent? I'm getting a thread issue error trying to call it. I found this answer from 2020 but I'm having trouble parsing the code in French. I've tried wrapping everything in the Current.Dispatcher.Invoke() but then my OK button is disabled (I assume because the thread is locked).

My code:

 

 

public static void OnRowCreated(RowChangedEventArgs args)
        {
            CreateLotWindow createLotWindow = new CreateLotWindow();
            createLotWindow.Owner = FrameworkApplication.Current.MainWindow;
            var vm = createLotWindow.DataContext as CreateLotWindowViewModel;
            if (createLotWindow.ShowDialog() == true)
            {
                string lotNo = vm.LotCount;
                QueuedTask.Run(()=> {
                     // do stuff with user input
                }
            }
        }

 

 

Thanks again.

0 Kudos