question

Michael-8660 avatar image
0 Votes"
Michael-8660 asked HuiLiu-MSFT edited

WPF DataContext Bindings on Multiple Pages

I have an application that has one window and a dozen pages. I have a class of bindable properties I want to use for bindings on several pages. I also need to access them often in the code behind.

I am not totally satisfied with my current solution. I instantiate the class of bindables as public static. I set up another routine as public static that declares the bindables as datacontext to the window and pages I need. This way they all share the same instance.

 public class dcx   // DataContext, bindable parameters
 {
     public static Bindables myBindables = new Bindables();

     public static void InitializeDataContext()
     {
         win.Main.DataContext = myBindables;
         pages.page1.DataContext = myBindables;
         pages.page2.DataContext = myBindables;
         pages.page3.DataContext = myBindables;
     }
 }

One of the drawbacks to this is that at design time none of the parameters are available. It makes design much more difficult. I guess I could declare the datacontext with d: and then at runtime it would get overridden.

It just all seems like there should be a better way.

windows-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@ Michael-8660. What does it mean that no parameters are available at design time? I don't quite understand the sentence I could declare the datacontext with d: and then at runtime it would get overridden. Does it mean setting the binding in xaml? Such as code:

  xmlns:viewModel = "clr-namespace:MvvmUnitTest.ViewModel"
     <Page.DataContext>
          <viewModel:StudentViewModel/>
     </Page.DataContext>
0 Votes 0 ·
Michael-8660 avatar image
0 Votes"
Michael-8660 answered HuiLiu-MSFT commented

By default every XAML file has line near the beginning: mc:Ignorable="d". Things proceeded d: are used by the rendering engine or compiler darning design time. During the compile to an executable they are ignored. When you right click a properties of a control and select Create Data Binding you get a dialog. When you do datacontext in C# there is no reference to whatever is in datacontext. When you do it the way that you did you will see the properties of what is in datacontext in the dialog. Hence, the dilemma.

The problem of doing what you did is that it instantiates StudentViewModel. Every page you do this on will look at a different instance. What I did was create a single instance. I pointed all datacontext binding to look at the same instance.

Note, I have not tried using d: for datacontext. I have used d: for other things. Every thing I have read says it should work for datacontext also.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@ Michael-8660. Does your MVVM framework use Nuget packages? Or did it manually? For details of d:DataContext in MVVM Light, you could refer to here.

0 Votes 0 ·

The line mc:Ignorable="d" is from the standard framework of a new window or page when creating an WPF. No Nugets involved.

Please note: https://docs.microsoft.com/en-us/dotnet/desktop/wpf/advanced/mc-ignorable-attribute?redirectedfrom=MSDN&view=netframeworkdesktop-4.8

MVVM Light was an experiment that has long been abandon. It does not need to be depreciated since it was never elevated to begin with. It is just one of hundreds of abandon experiments. We have a couple of apps that use MVVM Light. Untangling them has proven so difficult we have just mostly started over.

0 Votes 0 ·

Hi,@ Michael-8660. If you want to use design-time d: to declare the DataContext, you need to implement it in MVVM. You could choose the MVVM framework according to your needs.

0 Votes 0 ·
Michael-8660 avatar image
0 Votes"
Michael-8660 answered HuiLiu-MSFT edited

The letter is not as important as mc:Ignorable. I tried it and it does not work. I put in the DataContext lines in for development. I comment them out when a run. Kind of sledge hammer but it works.

Back to the original question. What is the best way to do DataContext Bindings on Multiple Pages?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

We usually use DataContext to bind the same ViewModel instance to different pages in the code, just like your code. It can update the data of multiple pages simultaneously.

--- One of the drawbacks to this is that at design time none of the parameters are available.

What is the purpose of the parameters you want to use?

0 Votes 0 ·