c# - Equivalent to WPF DependencyProperties in Prism ViewModels -




i understand standard way in wpf expose custom property in xaml define dependencyproperty in view’s code-behind.

however, works dependencyobjects, such usercontrol. yet, in clean prism fashion, code-behind (i.e., class deriving usercontrol) empty, , deal logic in view model, derives bindablebase, not child class of dependencyobject.

consider following xaml fragment:

<mynamespace:mycustomview myproperty={binding} /> 

the core of mycustomviewmodel is

private string myproperty; public string myproperty {   { return myproperty; }   set { setproperty(ref myproperty, value); } 

i’m still relatively new prism. do expose myproperty, defined in mycustomviewmodel can set in xaml tag similar above?


update

following @mm8’s answer , our discussion in corresponding comments, developed minimal (non-)working example of have in mind. summary first:

  • data model list of objects.
  • shell must display each of these objects means of custom user control object type.

a) shell

a.1) xaml

the xaml straightforward.

<window x:class="myproject.views.mainwindow"         name="mainwindowname"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:prism="http://prismlibrary.com/"         xmlns:mynamespace="clr-namespace:myproject.views"         prism:viewmodellocator.autowireviewmodel="true"         title="{binding title}" height="350" width="525">   <itemscontrol itemssource="{binding stringcollection, elementname=mainwindowname}">     <itemscontrol.itemtemplate>       <datatemplate>         <mynamespace:myusercontrol mytargetproperty="{binding}" />       </datatemplate>     </itemscontrol.itemtemplate>   </itemscontrol> </window> 

a.2) code-behind

the code-behind contains data model definition; in reality, i’d define in models namespace, of course.

using system.collections; using system.windows;  namespace myproject.views {   /// <summary>   /// interaction logic mainwindow.xaml   /// </summary>   public partial class mainwindow : window {     public mainwindow() {       initializecomponent();        stringcollection = new arraylist();       stringcollection.add("string 1");       stringcollection.add("string 2");       stringcollection.add("string 3");     }      private arraylist stringcollection;     public arraylist stringcollection {       { return stringcollection; }       set { stringcollection = value; }     }   } } 

a.3) view model

the view model standard 1 provided prism code templates.

using prism.mvvm;  namespace myproject.viewmodels {   public class mainwindowviewmodel : bindablebase {     private string _title = "prism unity application";     public string title {       { return _title; }       set { setproperty(ref _title, value); }     }      public mainwindowviewmodel() {     }   } } 

b) custom user control

this fun starts. in end, i’d have access mytargetproperty in myusercontrolviewmodel, since want invoke sophisticated program logic on depends on other work data model, , not placed in code-behind.

b.1) xaml

very naive; contains label.

<usercontrol x:class="myproject.views.myusercontrol"              name="usercontrolname"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:prism="http://prismlibrary.com/"                           prism:viewmodellocator.autowireviewmodel="true">     <label content="{binding mytargetproperty, elementname=usercontrolname}" background="aliceblue"/> </usercontrol> 

b.2) code-behind

this declare target property dependencyproperty, suggested in @mm8’s answer.

using system.windows; using system.windows.controls;  namespace myproject.views {   /// <summary>   /// interaction logic myusercontrol   /// </summary>   public partial class myusercontrol : usercontrol {     public myusercontrol() {       initializecomponent();     }      public static readonly dependencyproperty mytargetpropertyproperty = dependencyproperty.register("mytargetproperty", typeof(string), typeof(myusercontrol));     public string mytargetproperty {       { return (string)getvalue(mytargetpropertyproperty); }       set { setvalue(mytargetpropertyproperty, value); }     }   } } 

b.3) view model

the view model defines source property.

using prism.mvvm;  namespace myproject.viewmodels {   public class myusercontrolviewmodel : bindablebase {     public myusercontrolviewmodel() {     }      private string mysourceproperty;     public string mysourceproperty {       { return mysourceproperty; }       set { setproperty(ref mysourceproperty, value); }     }   } } 

i can’t life of me figure out how access values set in mainwindow’s itemtemplate within myusercontrol’s view model.

only target (view) properties must dependency properties. able bind to such property, must dependency property myproperty in case:

<mynamespace:mycustomview myproperty="{binding sourceproperty}" /> 

a source property in view model may plain clr property:

public string sourceproperty { get; set; } 

so view models don't have (and shouldn't!) inherit dependencyobject views should.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -