c# - How do I bind a datatrigger in xaml to a code-defined dependency property? -
my code behind window defines dependency property, "active"...
public partial class mainwindow : window { public mainwindow() { initializecomponent(); } public bool active { { return (bool) getvalue(activeproperty); } set { setvalue(activeproperty, value); } } public static readonly dependencyproperty activeproperty = dependencyproperty.register("active", typeof(bool), typeof(mainwindow), new uipropertymetadata(false)); }
and bind property using 2 checkboxes in xaml. want change fill of rectangle based on property. how can make work?
<window x:class="wpftest.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525" datacontext="{binding relativesource={relativesource self}}"> <stackpanel> <checkbox ischecked="{binding active}" /> <checkbox ischecked="{binding active}" /> <rectangle fill="gray" width="50" height="50"> <rectangle.style> <style targettype="rectangle"> <style.triggers> <datatrigger binding="{binding active}" value="true"> <setter property="fill" value="green" /> </datatrigger> </style.triggers> </style> </rectangle.style> </rectangle> </stackpanel> </window>
checking 1 box auto-checks other, not change rectangle color :(
locally set properties override style set properties, need remove locally set 1 , set default value in style instead:
<rectangle width="50" height="50"> <rectangle.style> <style targettype="rectangle"> <setter property="fill" value="gray" /> <style.triggers> <datatrigger binding="{binding active}" value="true"> <setter property="fill" value="green"/> </datatrigger> </style.triggers> </style> </rectangle.style> </rectangle>
Comments
Post a Comment