View not showing in a composite view

Jan 11, 2009 at 7:32 AM
Edited Jan 11, 2009 at 7:37 AM
Hi,
I'm trying to follow the StockTrader example of OrdersCompositeView and OrdersDetailsView. So I have MyCompositeViewPresentationModel which have a property:

public 

object MyDetailsView { get; private set; }
In MyCompositeViewPresentationModel I have the following code:

public 
MyCompositeViewPresentationModel (IMyCompositeView myCompositeView,  IMyDetailsViewPresentationModel detailsViewPresentationModel )
{

_detailsViewPresentationModel = detailsPresentationModel;
_view = myCompositeView;
this.MyDetailsView = _detailsViewPresentationModel .View;
_view.Model =
this;

 

  }

 

 

 And then, in the XAML of MyCompositeView I have the following binding:
<Expander Header="Details" Content="{Binding MyDetailsView}" />  

Everythin runs fine. MyCompositeView is loaded into the region and I can see it. In the debugger I can see MyDetailsView is loaded and activated. But, I can't see it...When I load MyDetailsView directly to a region it is fine so I know the view is ok. My question is what am I missing? I know it's hard to tell without the full code but if anyone has any idea I'll be greatful.
Thank you,
Ken

 

 

 

Jan 12, 2009 at 10:11 PM
Any ideas? I'm still ponding my head against thw wall with it... The composite view example in the StockTrader app seems to be pretty straight forward and still my details view is refusing to show up...
Please help!!
Thank you,
Ken
Jan 12, 2009 at 11:02 PM

I have found that WPF doesn't know if "bound to" properties change unless they notify it.   The following should give you something to try: 

 public object MyDetailsView
 {
        get { return _myDetailsView; }
        set {
             _myDetailsView = value; 
             OnPropertyChanged("MyDetailsView");
        }
 }
 

   public class  YourModel : INotifyPropertyChanged
    {
        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null)
                Handler(this, new PropertyChangedEventArgs(propertyName));
        }

 

Jan 12, 2009 at 11:46 PM
Hi Bill,
Thank you. I followed your advice and implement the INotifyPropertyChanged with no luck :(. I see thye debugger hitting the event handler but my details/child view is stil. not showing.
I must say, that though it seems very logical to use this notification mechanism, it is not being used in stock trader for the purpose of updating a child view in a composite view. I have no idea why...
Thank you,
Ken
Jan 13, 2009 at 12:00 AM
Hmm, as you suggested this might be difficult to help you with without the source...

Perhaps the following link will show you an alternate way of loading your detail view?  I use the PresentationModel however I make it the presenters responsibility to update the model - the model is shared across multiple views.   I provide blog info, source code and a short video clip.

http://www.global-webnet.net/blogengine/post/2008/12/18/Silverlight-CompositeWPF-Prism-regions.aspx


Jan 13, 2009 at 6:37 PM

Thank you Bill. I went over you blog posts and sample code but unfortunatly I still can't figure out my problem. So below is my full code. I'll be greateful if anyone could review it an let me know why isn't my child view showing inside the composite view?
Thank you,
Ken

App.xaml and boot strapper are the standard one. The only modified line of code I have in the bootstarpper is:

protected override IModuleEnumerator GetModuleEnumerator()

        {

            //Return New ConfigurationModuleEnumerator()

            return new StaticModuleEnumerator().AddModule(typeof(Module));

 

        }

Module.cs:

public class Module :IModule

    {

        private IRegionManager _regionManager;

        private readonly IUnityContainer _container;

 

        public Module(IUnityContainer container, IRegionManager regionManager)

        {

            _container = container;

            _regionManager = regionManager;

        }

 

        public void Initialize()

        {

            this.RegisterViewsAndServices();

            try

            {

                IMyCompositeViewPresentationModel presentationModel = _container.Resolve<IMyCompositeViewPresentationModel>();

                IRegion mainregion = _regionManager.Regions["MainRegion"];

                mainregion.Add(presentationModel.View);

                mainregion.Activate(presentationModel.View);

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

 

        protected void RegisterViewsAndServices()

        {

            _container.RegisterType<IMyCompositeViewPresentationModel, MyCompositeViewPresentationModel>();

            _container.RegisterType<IMyCompositeView , MyCompositeView>();

            _container.RegisterType<IChildViewPresentationModel, ChildViewPresentationModel>();

            _container.RegisterType<IChildView, ChildView>();

        }

    }

 

IMyCompositeView.cs

public interface IMyCompositeView

    {

        IMyCompositeViewPresentationModel Model { set; }

    }

 

MyCompositeView.xaml

<UserControl x:Class="MyModule.MyCompositeView"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Height="300" Width="300">

    <Grid>

        <StackPanel>

            <Label>My copositeView</Label>

            <Expander Header="MyChildView" Content="{Binding ChileView}"></Expander>

        </StackPanel>

    </Grid>

</UserControl>

 

MyCompositeView.xaml.cs

public partial class MyCompositeView : UserControl, IMyCompositeView

    {

        public MyCompositeView()

        {

            InitializeComponent();

        }

 

        #region IMyCompositeView Members

 

        public IMyCompositeViewPresentationModel Model

        {

            set { DataContext = value; }

        }

 

        #endregion

    }

 

 

IMyCompositeViewPresentationModel.cs

public interface IMyCompositeViewPresentationModel

    {

        IMyCompositeView View { get; }

    }

 

MyCompositeViewPresentationModel .cs

public class MyCompositeViewPresentationModel : IMyCompositeViewPresentationModel, INotifyPropertyChanged

    {

        private IMyCompositeView _view;

        private IChildViewPresentationModel _childViewPresentationModel;

 

        public MyCompositeViewPresentationModel(

            IMyCompositeView view,

            IChildViewPresentationModel childViewPresentationModel

            )

        {

            this._view = view;

            this._childViewPresentationModel = childViewPresentationModel;

            this.ChildView = this._childViewPresentationModel.View;

            this._view.Model = this;

        }

 

        private object childView;

        public object ChildView

        {

            get { return _childViewPresentationModel; }

            set

            {

                childView = value;

                this.OnPropertyChanged("ChildView");

            }

        }

 

        #region IMyCompositeViewPresentationModel Members

 

        public IMyCompositeView View

        {

            get { return this._view; }

        }

 

        #endregion

 

        #region INotifyPropertyChanged Members

 

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        void OnPropertyChanged(string propertyName)

        {

            try

            {

                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

            }

            catch(Exception ex)

            {

                throw ex;

            }

        }

 

        #endregion

    }

 

IChildView.cs

public interface IChildView

    {

        IChildViewPresentationModel Model { set; }

    }

ChildView.xaml

<UserControl x:Class="MyModule.ChildView"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Height="300" Width="300">

    <Grid>

         <Label>My Child View content</Label>  

    </Grid>

</UserControl>

 

ChildView.xaml.cs

public partial class ChildView : UserControl, IChildView

    {

        public ChildView()

        {

            InitializeComponent();

        }

 

        #region IChildView Members

 

        public IChildViewPresentationModel Model

        {

            set { DataContext = value; }

        }

 

        #endregion

    }

 

IChildViewPresentationModel.cs

public interface IChildViewPresentationModel

    {

        IChildView View { get; }

    }

ChildViewPresentationModel.cs

public class ChildViewPresentationModel : IChildViewPresentationModel, INotifyPropertyChanged

    {

        private IChildView _view;

        public ChildViewPresentationModel(IChildView view)

        {

            this._view = view;

            this._view.Model = this;

        }

        #region IChildViewPresentationModel Members

 

        public IChildView View

        {

            get { return this._view; }

        }

 

        #endregion

 

        #region INotifyPropertyChanged Members

 

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        void OnPropertyChanged(string propertyName)

        {

            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

        }

 

 

        #endregion

    }

Jan 13, 2009 at 10:34 PM



MyCompositeView.xaml

<UserControl x:Class="MyModule.MyCompositeView"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Height="300" Width="300">

    <Grid>

        <StackPanel>

            <Label>My copositeView</Label>

            <Expander Header="MyChildView" Content="{Binding ChileView}"></Expander>

        </StackPanel>

    </Grid>

</UserControl>

In the above the BindingPath should be Content="{Binding ChildView}" right? As we don't have a Property called ChileView in MyCompositeViewPresentationModel


Jan 13, 2009 at 10:56 PM
OUCH!!
Good catch :).
Thank you so much. I've beed banging my head against the wall for couple of days...
Ken