Miguel
Miguel

Reputation: 947

Bind different Properties from a ViewModel depending using triggers

I am trying to achieve a simple thing, or i think it's simple but i don't know if it's possible...

Imagine the following scenario... In the ViewModel i have one Property called SelectedProduct and other one called NewProduct (both of the same type, the model).

In my MainWindow we have a ListView and two buttons, one is the Add button and the other is the Update button (and this one will update the Selected Item in the ListView), and both will open the same window but will do different things.

I am using Commands in the buttons, so i think i can use one Command if have clicked in the Add button of the MainWindow or use the other Command.

So if we click in the Add button, the controls (TextBoxes and ComboBoxes) in the second window will bind the NewProduct property, and if we click in the Update button the second window will bind the SelectedProduct property.

Is there any way to achieve this?

Thanks in advance

Upvotes: 0

Views: 176

Answers (3)

Bathineni
Bathineni

Reputation: 3496

Have a look at following view model.

You can create a new property which will be binded to your view and you can change the property reference based on your button command.

In the following code i have created a edited property (Binded to view) and i have assigned the Newproduct to it on add button command execute and assigned SelectedProduct while updating..

 class Viewmodel : ViewModelBase
    {

        private void AddCommandExecute(object o)
        {
            // your logic
            EditedProduct = NewProduct;
        }

        private void UpdateCommandExecute(object o)
        {
            // your logic
            EditedProduct = SelectedProduct;
        }


        private Product _selectedProduct;

        public Product SelectedProduct
        {
            get { return _selectedProduct; }
            set
            {
                _selectedProduct = value;
                OnPropertyChanged("SelectedProduct");
            }
        }

        private Product _newProduct;

        public Product NewProduct
        {
            get { return _newProduct; }
            set
            {
                _newProduct = value;
                OnPropertyChanged("NewProduct");
            }
        }

        private Product _editedProduct;

        public Product EditedProduct
        {
            get { return _editedProduct; }
            set
            {
                _editedProduct = value;
                OnPropertyChanged("EditedProduct");
            }
        }

    }

Upvotes: 0

fatty
fatty

Reputation: 2513

Two methods I can see that will work for this scenario would be to use two different commands as you stated that will each call the Window differently.

The first option would be to create either two constructors for the Window (one for Add, and the other for Update), or just create one that passes in an enum value as its parameter.

public void Window2(DisplayMode mode)

Where DisplayMode is an enum with two values (Add and Update)

or

public void Window2() // The default 'Add' window
{
}

public void Window2(ModelObject instance) // The 'Update' window
{
}

The second option would be to make the constructor for the Window private, and create static methods on the class to create an instance of the Window and display it to the user.

This could be used like

Window2.UpdateItem(selectedItem)

With each of these methods setting the DataContext for the window instance to the object you wanted edited (either to a new instance of the object, or to the instance that was passed in to the constructor)

Upvotes: 0

Richard Szalay
Richard Szalay

Reputation: 84734

Why don't you just create a single EditedProduct property and give that the appropriate value?

Upvotes: 2

Related Questions