c# - WPF change background of a control with an attached property -
I need to change the background of the label and button when a boolean variable (the default color is returned to the wrong). So I have written an attached property so far this looks like this:
public class BackgroundChanger: DependencyObject {#region dependency attribute // condition public static bool GetStatus (DependencyObject obj) {return (bool Obj.GetValue (StatusProperty); } Public static zero SetStatus (dependency object obj, boolean value) {obj.SetValue (StatusProperty, value); } Public static Read Only DependencyProperty StatusProperty = DependencyProperty.RegisterAttached ("status", typeof (bool), typeof (BackgroundChanger), new UIPropertyMetadata (incorrect, OnStatusChange)); #endregion Private Static Zero OnStatusChange (dependency object obj, DependencyPropertyChangedEventArgs e) {var element = obj; If (element! = Null) {if ((bool) e.NewValue) element.Background = Brushes.LimeGreen; Second element Background = default (brush); }}}
And I use it like this:
This works fine when according to the variable Color test
is set in viewmodel, background color changes for LimeGreen < P> My questions :.
LimeGreen
is hard coded I would like to set that color (as well as the default color) in XML so that I can decide that two of the background Which two colors change between the colors How can I go about doing this?
You can have multiple attachments that are easy to access, go to static .
and set.
methods for which you supply the dependency object
, which you want to assume an associated asset operated
Public Square BackgroundChanger :. DependencyObject {// property brushes background // type "propdp", press "tab" and complete filling Private Static Zero OnStatusChange (DependencyObject obj, DependencyPropertyChangedEventArgs e) {Making var element = obj; If (element! = Null) {if ((bool) e.NewValue) element.Background = GetBrush (obj); // Another affiliate property value get other elements. Background = default (brush); }}}
XAML will look like this
in
Comments
Post a Comment