c# - Binding Opacity values to a static property; -
i trying make desktop overlaying application (think rainmeter) , because backgrounds can change, want able change general colors , alpha values text in application.
so in settings menu want slider onvaluechanged sets property in static class , lots , lots of controls have opacity bound property. make complicated (maybe?) application has multiple windows open simultaneously. have little experience binding , can't work.
my code far:
visualsettings.cs
namespace projectsidebar { public class visualsettings { public static double opacity { get; set; } } }
mainwindow.xaml
<window x:class="projectsidebar.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:psb="clr-namespace:projectsidebar" title="mainwindow" height="1080" width="300" resizemode="noresize" showintaskbar="false" windowstyle="none" closing="window_closing_1" loaded="window_loaded" background="transparent" > <window.resources> <psb:visualsettings x:key="visualsettings"/> </window.resources> <grid> <textblock x:name="clocktb" horizontalalignment="left" textwrapping="wrap" text="22:22:22" verticalalignment="top" height="84" width="300" cursor="none" foreground="white" fontsize="48" fontfamily="batmanforeveralternate" textalignment="center" opacity="{binding source={staticresource visualsettings} , path=opacity}" margin="0,22,0,0" rendertransformorigin="0.5,0.5"> <textblock.rendertransform> <transformgroup> <scaletransform scaley="1.5"/> <skewtransform/> <rotatetransform/> <translatetransform/> </transformgroup> </textblock.rendertransform> </textblock> <slider x:name="testslider" horizontalalignment="left" margin="10,1052,0,0" verticalalignment="top" rendertransformorigin="0.5,0.611" width="172" foreground="#ff122268" valuechanged="testslider_valuechanged" largechange="0.1" smallchange="0.01" maximum="1" value="0.65"/> </grid> </window>
mainwindow.xaml.cs
private void testslider_valuechanged(object sender, routedpropertychangedeventargs<double> e) { visualsettings.opacity = testslider.value; }
can me out?
kind regards, roxas
you can bind static properties using x:static
issue x:static don't support property changed mechanism i.e. if static property changes, won't updated on ui.
however, wpf 4.5 have support using event staticpropertychanged
. need make sure whenever static property changes raise event ui gets updated.
also syntax binding static properties bit different in case:
"{binding path=(local:visualsettings.opacity), mode=twoway}"
sample can found here.
Comments
Post a Comment