Ron16
Ron16

Reputation: 473

Hybrid MVVM implementation for a PropertyGrid (in DevExpress)

I need your help! Following is basically what I have in my main XAML view :

<Button x:Name="button1" Content= "{Binding Customer1, Mode=TwoWay}"   Margin="271,52,103,106" Click="button1_Click" />

The code-behind of the main XAML (Code-behind, since it's not a 100% pure MVVM, and a rather hybrid one) goes like this :

public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MyViewModel();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {


        DXDialog d = new DXDialog("Information", DialogButtons.OkCancel,true);
        d.Content = new PropertyGrid();
        d.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
        d.Owner = this;
        d.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
        var result = d.ShowDialog();
        if (result == true)
        {

        }


    }

As you can see, I have a Button whose content is bound to a String property in the ViewModel Class. Upon Clicking the button, I'm opening a DXDialog which contains a PropertyGrid with the Properties of the ViewModel class. Let me show you my ViewModel Class below :

public class MyViewModel : ViewModelBase, INotifyPropertyChanged
    {
        Customer currentCustomer;

        protected string _customer1;
        public string Customer1 {
            get { return this._customer1; }
            set { this.SetProperty(ref this._customer1, value, "Customer1"); }
        }


        public MyViewModel()
        {
            //Customers = new ObservableCollection<Customer>();
            //Customers.Add(new Customer() { Name = "Name1" });
            Customer1 = "ABC";

        }
}

In the Dialog I'm being able to edit the value of the property but don't yet know how I can save it in a way that it immediately reflects even on the button of the main View {Reflects everywhere it must be bound to, I mean}. I can see the execution coming to the following line in the main code behind

 if (result == true)
            {

            }

But I don't know how to get the edited values and plug them into the right place.

Basically, My requirement is to have multiple controls (Buttons, in this case) bound to multiple instances of a ViewModel class, and then, upon clicking the buttons, I should be able to edit those specific ViewModel instances inside the PropertyGrid of the DXDialogue, and after clicking "Ok", the changes should reflect on the relevant buttons as well.

-Ron

Upvotes: 0

Views: 151

Answers (1)

Uranus
Uranus

Reputation: 1690

To display ViewModel's properties in the PropertyGrid, assign the ViewModel to its SelectedObject property,and make sure that the ShowProperties option is set to All.

Changes will be reflected in buttons bound to the ViewModel only of you use one and the same ViewModel instance in the main and the dialog windows.

var grid = new PropertyGrid();
grid.SelectedObject = this.DataContext;
grid.ShowProperties = ShowPropertiesMode.All;
d.Content = grid;

Upvotes: 1

Related Questions