WPF/XAML: Switch style based on custom property -
i have wpf user control following xaml
<usercontrol x:class="scheduler.itembox" 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="40" d:designwidth="150" minheight="40" minwidth="75" verticalalignment="top"> <border borderbrush="cornflowerblue" borderthickness="1" cornerradius="5" name="border"> <border.background> <lineargradientbrush endpoint="0.5,1" startpoint="0.5,0"> <gradientstop color="white" offset="0"/> <gradientstop color="#ffc0d3ea" offset="1"/> </lineargradientbrush> </border.background> <grid margin="2,0" name="grid"> <grid.columndefinitions> <columndefinition /> <columndefinition /> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="20" maxheight="20" minheight="20" /> <rowdefinition minheight="20" /> </grid.rowdefinitions> <label content="00:00" fontweight="bold" name="fromtime" padding="5,0,0,0" verticalcontentalignment="center" /> <label content="01:30" grid.column="1" horizontalcontentalignment="right" name="totime" padding="0,0,5,0" verticalcontentalignment="center" /> <textblock grid.columnspan="2" grid.row="1" name="movietitle" padding="5,0" text="item1" textwrapping="wrap" /> </grid> </border>
and user control class looks this
namespace scheduler public class itembox public property selected boolean end class
end namespace
now is, when change property selected true following: - set border borderbrush black - set border borderthickness 2 - set grid margin 1
i accomplish defining "selected" style in usercontrol overrides default styles when selected property set true.
i know has style trigger , defining custom attached property. can't seem make work way want to.
the first issue selected property not "observable". means watching property changes (such style trigger or binding), never notified changed.
you either need implement inotifypropertychanged or make property dependency property. doesn't need attached property, can bind property using relativesource, if needed.
the second issue usercontrol doesn't have style, @ least not default. if set usercontrol.style property, cannot change content. more done using custom control, , best bet accomplish want.
Comments
Post a Comment