none
Controlling Window Behavior Using Controls In Seperate Windows

    Question

  • I saw a helpful method that using data binding and a PropertyChangedEventHandler, I was able to link the behavior of a window with a checkbox.  I would like to extend that capability with other controls that effect the behavior of a second window.

    For example, Window 1 has controls that can control window.height and window.width though the effects of those setting occur in Window 2.  Is it a matter of having lots of properties in Window 2 and bind them to the controls in Window 1 or have numerous static variables that can be accessed by both windows?  I don't think having all the controls as public is a good idea.  I also can't have a second instance of either Window as it involves streaming video through an ActiveX control hosted by WPF.

     

    Hope that was all clear.

     

    Thanks much

    Wednesday, August 18, 2010 8:33 PM

Answers

All replies

  • You can pass an object (DependencyObject or INotifyPropertyChanged interface implementer) to the second window. This can serve as the view model of the second window. After assigning it as a DataContext of the second window, you can bind the properties of the second window.

    Now whenever you would change the properties of this object in the parent window, they would be reflected in child window.

    For dataBinding you can refer:

    http://msdn.microsoft.com/en-us/library/ms752347.aspx

    Thanks,

    Muhammad

    shujaatsiddiqi.blogspot.com


    Shujaat Siddiqi
    Wednesday, August 18, 2010 8:51 PM
  • You can pass an object (DependencyObject or INotifyPropertyChanged interface implementer) to the second window. This can serve as the view model of the second window. After assigning it as a DataContext of the second window, you can bind the properties of the second window.

    Now whenever you would change the properties of this object in the parent window, they would be reflected in child window.

    For dataBinding you can refer:

    http://msdn.microsoft.com/en-us/library/ms752347.aspx

    Thanks,

    Muhammad

    shujaatsiddiqi.blogspot.com


    Shujaat Siddiqi
    The second Window has an ActiveX control in a user control.  That is also wrapped in a WindowsFormsHost control.  The properties of the ActiveX control are what I am seeking to control from the controls in the primary window.  Those are a lot of properties (about 15) made up of numeric and boolean to bind.  Are you suggesting that is possible?
    Wednesday, August 18, 2010 9:05 PM
  • Hi alleys,

    I think Muhammad's suggestion is possible. Such as create a ViewModel between two windows, and set the bindings with the controls in the first window to the ViewModel, and meanwhile set the bindings for the controls in the second window. The DataBinding engine can update and sync the data between the two windows:

     

    Since the ActiveX control does not have the Dependency Property, so we can not set a DataBinding for them to the ViewModel in the Window 2. We need to create the Dependency properties in the View Model that bind the properties to the ActiveX control.

    A sample that use a Windows.Forms.Button instead of the ActiveX in the Window:
    http://cid-51b2fdd068799d15.office.live.com/self.aspx/.Public/Samples%5E_2010/20100819%5E_ControlSeperate%20Window.zip

    Sincerely,

    Bob Bao

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, August 19, 2010 7:00 AM
  • A sample that use a Windows.Forms.Button instead of the ActiveX in the Window:
    http://cid-51b2fdd068799d15.office.live.com/self.aspx/.Public/Samples%5E_2010/20100819%5E_ControlSeperate%20Window.zip

    The link is experiencing problems:  Windows Live can not launch from that location.  What I am learning is that I need to create a dependency object that can contain multiple properties that can be written to/read by the ActiveX Control?
    Thursday, August 19, 2010 1:04 PM
  • Hi,

    Yes, you are right. The sample I have updated to another place , http://www.4shared.com/file/ivF0SHXv/20100819_ControlSeperate_Windo.html

    Please try again.

    Sincerely,

    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by alleyes Thursday, August 19, 2010 1:32 PM
    Thursday, August 19, 2010 1:14 PM
  • Hi alleys,

    I think Muhammad's suggestion is possible. Such as create a ViewModel between two windows, and set the bindings with the controls in the first window to the ViewModel, and meanwhile set the bindings for the controls in the second window. The DataBinding engine can update and sync the data between the two windows:

    Since the ActiveX control does not have the Dependency Property, so we can not set a DataBinding for them to the ViewModel in the Window 2. We need to create the Dependency properties in the View Model that bind the properties to the ActiveX control.

    I understand the limitation of Window 2.  Is there still a way to send information back to Window 1?  That is, Window 1 has all the controls and updates properties in Window 2.  If there is textbox in Window 1, can Window 2 update it?
    Thursday, August 19, 2010 9:17 PM
  • Hi,

    It can, please see my sample, I used the TwoWay binding for each property. So one value changed in the Window2, the Binding engine can update the ViewModel property first, and the ViewModel can notify the property in the Window 1 through the INotifyPropertyChanged interface in the ViewModel.

    Sincerely,

    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, August 20, 2010 3:11 AM
  • I added a textbox as an indicator to Window 1  I used the click event of the button in Window 2 to send back a property back to the text box of Window 1.  Using the available properties in the example, as I type in the first text box in Window 1 the button text in Window 2 gets updated as expected AND the added text box in Window 1.  Not the effect I was looking for.  The analogy of this issue is the desire to send back properties from an ActiveX object and pass that back to the objects in the controlling window (Window 1).  The binding engine is not doing that at least the way I set it up.
    Friday, August 20, 2010 5:01 PM
  • Hi alleyes,

    Based on my sample, I bound the two windows by the middle model in TwoWay mode. Since the property of the ActiveX is not the dependency property, so we have to bind the property as the source value, and set the property in the view model as the tagert value.

    Ok, now we need to create a new textbox in the window 1 and bingd it value to the view model. Just add the TextBox in the XAML code of the window 1:

      <TextBox Text="{Binding ButtonText, Mode=OneWay}" Margin="10"/>
    

    Then you input the value in the first TextBox, the value can update the Button in the window 2 and meawhile update this TextBox in the window 1. The value can be sent back by the middle model properties.

    Sincerely,

    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by alleyes Monday, August 23, 2010 2:12 PM
    • Unmarked as answer by alleyes Monday, August 23, 2010 2:25 PM
    Saturday, August 21, 2010 4:00 PM
  • Hi Bob,

    Thanks for clarifying.  I understand that ActiveX controls can not be a dependency property.  However it does have properties that can be accessed by .NET objects.  In the Window2 code I can set in the SetBinding method the Source to be the object of my ActiveX control.

     

    For example in the Window 2 code:

    BindingOperations.SetBinding(VM, ViewModel.CameraNameProperty, new Binding("VideoName") { Source = this.axctlobject, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });

     

    VideoName is a property of the ActiveX control i.e. axctlobject.VideoName

     

    In the ViewModel code:

        public string CameraName
        {
          get { return (string)GetValue(CameraNameProperty); }
          set
          {
            SetValue(CameraNameProperty, value);
            OnPropertyChanged("CameraName");
          }
        }
        public static readonly DependencyProperty CameraNameProperty=
            DependencyProperty.Register("CameraName", typeof(string), typeof(ViewModel), new UIPropertyMetadata(string.Empty,
            (o, e) =>
            {
              (o as ViewModel).CameraName= (string)e.NewValue;
            }));

     

    In the Window 1 code:

    <TextBox Text ="{Binding CameraName, Mode=OneWay}" Margin="10"/>

     

    Is this viable?

     

    Thanks for help.

     

    Saturday, August 21, 2010 7:19 PM
  • Hi alleyes,

    There is one problem, does the property of the ActiveX control implement the INotifyPropertyChanged interface? Ok, I know, in default, the ActiveX doesnot implement it, so we have to help the control to notify the ViewModel. How to? Get the BindingExpression of this DataBinding via BindingOperations.GetBindingExpression, and invoke the BindingExpression.UpdateTarget method. Please call this method the property changed of the control, such as the TextChenged event.

    Sincerely,

    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by alleyes Monday, August 23, 2010 2:12 PM
    • Unmarked as answer by alleyes Monday, August 23, 2010 2:25 PM
    Monday, August 23, 2010 4:53 AM
  • Hi alleyes,

    There is one problem, does the property of the ActiveX control implement the INotifyPropertyChanged interface? Ok, I know, in default, the ActiveX doesnot implement it, so we have to help the control to notify the ViewModel. How to? Get the BindingExpression of this DataBinding via BindingOperations.GetBindingExpression , and invoke the BindingExpression.UpdateTarget method. Please call this method the property changed of the control, such as the TextChenged event.

    Sincerely,

    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Hi Bob,

    Thanks for your patience with me trying to resolve this.  I've implemented all as you've said.  It still doesn't work.  I can however, read a property of the ActiveX control as it IS a part of the Window2.  I am now trying to construct an example of my situation by combining your sample with that of the Win7 SDK WPF sample named HostingAxInWpfWithXaml.  I believe this will be the closest thing to my situation.
    Monday, August 23, 2010 3:02 PM