c# - why UserControl don't use binding and x:Name? -


i want use usercontrol such contentcontrol.

example, if checkbutton check, change isenalbe property of button.

but, dont execute.

why??????

show attached code!

== window.xaml ==

<window         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:local="clr-namespace:wpfapplication4" x:class="wpfapplication4.mainwindow"         title="mainwindow" height="350" width="525">   <grid>     <local:usercontrol1 >       <local:usercontrol1.buttons>         <button isenabled="{binding path=ischecked, elementname=checkbox}" height="50"/>         <button height="50"/>       </local:usercontrol1.buttons>     </local:usercontrol1>     <checkbox x:name="checkbox"/>   </grid> </window> 

== usercontrol.xaml ==

<usercontrol x:class="wpfapplication4.usercontrol1"              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 x:name="mainstack">   </stackpanel> </usercontrol> 

== usercontrol.xaml.cs ==

public partial class usercontrol1 : usercontrol     {         public observablecollection<button> buttons         {             { return (observablecollection<button>)getvalue(buttonsproperty); }             set { setvalue(buttonsproperty, value); }         }          public static readonly dependencyproperty buttonsproperty =             dependencyproperty.register("buttons", typeof(observablecollection<button>), typeof(usercontrol1), new propertymetadata(                 (depobj, args) =>                 {                     var dep = depobj usercontrol1;                     var newitem = args.newvalue observablecollection<button>;                     newitem.collectionchanged += dep.buttonsoncollectionchanged;                 }));          private void buttonsoncollectionchanged(object sender, system.collections.specialized.notifycollectionchangedeventargs args)         {             switch (args.action)             {                 case notifycollectionchangedaction.add:                     foreach (var newitem in args.newitems)                     {                         var input = newitem button;                         mainstack.children.add(input);                     }                     break;                 case notifycollectionchangedaction.reset:                     mainstack.children.clear();                     break;             }         }          public usercontrol1()         {             buttons = new observablecollection<button>();             initializecomponent();         }     } 

your button nested in custom control , has no direct access checkbox. add property wchich checkbox binds ischecked property

   <checkbox x:name="checkbox" ischecked="{binding ischecked}"/> 

and can refer checkbox's datacontext follows

  <button isenabled="{binding relativesource={relativesource ancestortype=grid} path=datacontext.ischecked}" height="50"/> 

Comments