w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
change main form controls by trigger event inside canvas

You can have your main window be binded to a singletone ViewModel holding the properties of the rectangles.

ViewModel

 public class MainWindowViewModel : INotifyPropertyChanged
{
    #region Singletone

    private static MainWindowViewModel _instance;

    private MainWindowViewModel()
    {

    }

    public static MainWindowViewModel Instance
    {
        get
        {
            if (_instance == null)
                _instance = new MainWindowViewModel();

            return _instance;
        }
    } 

    #endregion

    #region Properties

    private string _someInfo;
    public string SomeInfo
    {
        get 
        {
            return _someInfo; 
        }
        set
        {
            if (_someInfo != value)
            {
                _someInfo = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new
PropertyChangedEventArgs("SomeInfo"));
            }


        }
    }

    #endregion

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

In main window xaml

<TextBox Text="{Binding SomeInfo}"/>

Also set the view model as your main window data context (in main window constructor for exmaple)

this.DataContext = MainWindowViewModel.Instance;

Finally, from where you handle the click event of the rectangles (testTi_MouseLeftButtonDown), access the MainWindowViewModel instance and set it's properties accordingly.

MainWindowViewModel.Instance.SomeInfo = myRectangle.SomeInfo;

This will trigger the PropertyChanged event, which will update your control's on the main window.

If you are not familiar with the MVVM (Model, View. View Model) pattern you can read about it here

Hope this helps





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.