Binding an element's visibility to a toggleButton or checkBox
what preferred method binding visibility of 1 element checkbox or togglebutton's checked state? understand need write converter return value visible or collapsed based on checkbox state, boolean -- silverlight 3 doesn't include support wpf converter. used converter post:
http://stackoverflow.com/questions/1534208/binding-to-a-wpf-togglebuttons-ischecked-state
when try build project, xaml invalid. 'the name "booleantovisibilityconverter" not exist in namespace "http://schemas.microsoft.com/client/2007".
<usercontrol xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:class="visibilitytest.mainpage" width="640" height="480"> <usercontrol.resources> <booleantovisibilityconverter x:key="booleantovisibilityconverter" /> </usercontrol.resources> <canvas x:name="layoutroot" background="white"> <checkbox content="checkbox" name="checkbox"/> <rectangle fill="#ff847563" visibility="{binding ischecked, elementname=checkbox, converter={staticresource booleantovisibilityconverter}}" stroke="black" height="106" width="221" canvas.left="219" canvas.top="160"/> </canvas> </usercontrol>
and c#:
namespace visibilitytest { public class booleantovisibilityconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { if (targettype == typeof(visibility)) { var visible = system.convert.toboolean(value, culture); if (invertvisibility) visible = !visible; return visible ? visibility.visible : visibility.collapsed; } throw new invalidoperationexception("converter can convert value of type visibility."); } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { throw new invalidoperationexception("converter cannot convert back."); } public boolean invertvisibility { get; set; } } }option skip , write new click event each item want use "button," i'd keep stylistic stuff out of c# can. i'll adding , removing these elements frequency project develops, , don't want have touch code behind every time take button out of xaml.
missing here? should obvious?
you forgot add namespace references namespace , add namespace converter. here should like:
<usercontrol xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:visibilitytest" x:class="visibilitytest.mainpage" width="640" height="480"> <usercontrol.resources> <local:booleantovisibilityconverter x:key="booleantovisibilityconverter" /> </usercontrol.resources> <canvas x:name="layoutroot" background="white"> <checkbox content="checkbox" name="checkbox"/> <rectangle fill="#ff847563" visibility="{binding ischecked, elementname=checkbox, converter={staticresource booleantovisibilityconverter}}" stroke="black" height="106" width="221" canvas.left="219" canvas.top="160"/> </canvas> </usercontrol>
hope trick you.
tony
Silverlight > Designing with Silverlight
Comments
Post a Comment