wpf - UserControl accessing DependencyProperty via RelativeSource Self -
wpf - UserControl accessing DependencyProperty via RelativeSource Self -
i have been having problem using relativesource
self in usercontrol. have contructed 2 usercontrols thought identical in operation, 1 works , other doesn't. user controls consist of label content want set dependencyproperty
title of control. when set datacontext
of usercontrol relativesource
self, , bind label path=title
works fine. if bind label relativesource findancestor, ancestortype{x:type usercontrol}}, path=title
fails targetinvocationexception.
any thoughts on doing wrong.
this xaml of usercontrol works:
<usercontrol x:class="usercontrolbinding.controls.mylabelcontrol1" 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" datacontext="{binding relativesource={relativesource self}}"> <grid> <label content="{binding path=title}"></label> </grid> </usercontrol>
this xaml of code fails
<usercontrol x:class="usercontrolbinding.controls.mylabelcontrol2" 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:usercontrolbinding.controls" mc:ignorable="d" d:designheight="300" d:designwidth="300"> <grid> <label content="{binding releativesource={relativesource findancestor, ancestortype={x:type local:mylabelcontrol2}}, path=title}"></label> </grid> </usercontrol>
assuming code title property isn't causing problem (can't tell since it's not listed) else looks ok except spelling: releativesource
-> relativesource
wpf user-controls dependency-properties relativesource
Comments
Post a Comment