c# - Binding an element in a child View to a property of the parent ViewModel -
like title says, bind element in child view property of parent viewmodel.
is there caliburn.micro or default wpf xaml syntax this? mean, exact situation: bind parent viewmodel's property.
or should try achieve other way? so, best/easiest way it?
here basic caliburn.micro app example:
mainwindowviewmodel.cs
namespace bindingtoparentvmproperty.mvvm { public class mainwindowviewmodel { public childviewmodel child { get; set; } public string parentname { get; set; } public mainwindowviewmodel() { child = new childviewmodel(); parentname = "peter griffin"; } } }
mainwindowview.xaml
:
<usercontrol x:class="bindingtoparentvmproperty.mvvm.mainwindowview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d"> <grid width="200" height="200"> <contentcontrol name="child" /> </grid> </usercontrol>
childviewmodel.cs
:
using caliburn.micro; namespace bindingtoparentvmproperty.mvvm { public class childviewmodel { public bindablecollection<grandchildviewmodel> grandkids { get; set; } public string childname { get; set; } public childviewmodel() { childname = "stewie griffin"; grandkids = new bindablecollection<grandchildviewmodel> { new grandchildviewmodel {grandchildname = "stewie griffin jr."}, new grandchildviewmodel {grandchildname = "rupert griffin jr."} }; } } }
childview.xaml
:
<usercontrol x:class="bindingtoparentvmproperty.mvvm.childview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:ignorable="d" d:designheight="300" d:designwidth="300"> <stackpanel> <!-- need bind mainwindowviewmodel.parentname--> <textblock text="{binding path=howtobindto_parentname}"/> <listview name="grandkids" /> </stackpanel> </usercontrol>
grandchildviewmodel.cs
:
namespace bindingtoparentvmproperty.mvvm { public class grandchildviewmodel { public string grandchildname { get; set; } } }
grandchildview.xaml
:
<usercontrol xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" x:class="bindingtoparentvmproperty.mvvm.grandchildview" mc:ignorable="d" d:designheight="300" d:designwidth="300"> <stackpanel> <textblock x:name="grandchildname" /> <!-- need bind childviewmodel.childname --> <textblock text="{binding path=howtobindto_childname}"/> </stackpanel> </usercontrol>
it seems straightforward suggestion keep record of parent on child viewmodel
(e.g. parent
property). since want have viewmodels
tied respective views
, think become conceptually quite tricky if start bind properties , include information beyond scope of bound viewmodel
.
also, whilst may not of concern particular project you're working on, if start including properties other usercontrols
, you're implicitly creating links , dependencies, may come inconvenience if wish re-use controls elsewhere in non-hierarchical context.
that said, traverse ui tree required datacontext
(viewmodel
) parent control, , bind desired property using existing wpf
syntax, kollegos
suggests, using relativesource
, ancestortype
property.
in case, might this:
<usercontrol x:class="bindingtoparentvmproperty.mvvm.childview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:yourprojectnamespacecontainingparentview" mc:ignorable="d" d:designheight="300" d:designwidth="300"> <stackpanel> <!-- set our datacontext bind parent mainwindowviews datacontext (viewmodel) --> <textblock text="{binding path=parentname}" datacontext="{binding relativesource={relativesource findancestor, ancestortype={x:type local:mainwindowview}}, path=datacontext}"/> <listview name="grandkids" /> </stackpanel>
Comments
Post a Comment