.net - Bind the foreground color of a TextBlock to local variable -
i want bind foreground color of textblock local variable. if variable has no value (=null) foreground color should black, if variable not null, foreground color should black or whatever. ist possible solve via binding?
you can utilize value converter first define converter this
[valueconversion (typeof(object), typeof(solidcolorbrush))] public class objecttobrushconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { if (value == null) homecoming new solidcolorbrush(colors.black); homecoming new solidcolorbrush(colors.red); } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } }
then in xaml file define converter resource
<window.resources> <local:objecttobrushconverter x:key="objecttobrushconverter"/> </window.resources>
then bind property , provide converter
<textbox name="textb" text="hello" foreground="{binding path=myobject, converter={staticresource resourcekey=objecttobrushconverter}}">
check out msdn on value converter http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx
of course of study assuming defined variable public property , of type object in example
.net wpf binding
No comments:
Post a Comment