WordPress database error: [INSERT, UPDATE command denied to user '51213-2'@'10.10.20.44' for table 'wp_options']INSERT INTO `wp_options` (`option_name`, `option_value`, `autoload`) VALUES ('_transient_doing_cron', '1714663216.0780038833618164062500', 'yes') ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)
<\/a>There is two different ways to make a property bindable: implementing INotifyPropertyChanged <\/em>is the most known solution, the other one is to create a event named PropertyName<\/em>Changed. <\/p>\n In WPF 4.0 there is no way to create a static property which can be used by a binding with property change notifications. <\/p>\n In this post, part of a serie on the WPF 4.5 new features<\/a>, we’ll see how it can now be done!<\/p>\n <\/p>\n As pointed out in the documentation page, there is, as for instance properties, two solution to achieve that:<\/p>\n I first think that this will be an easy demo to build: I just have to use the x:Static extension as a source in a Binding and tada this is over! But life isn’t easy: it does not works.<\/p>\n On the contrary, I had to put an instance object on which my static property is defined in the resource and use it as the source of the Binding<\/strong>. [csharp] \/\/Color property is here, let’s see later how to define it… Then I put an instance of it in the application resources: And I create a binding on the static property of this instance: Sam Bent from the WPF team explained me via emails why the static markup extension cannot be used for this scenario. Here is a quote of his mail:<\/p>\n It\u2019s not so strange. In general, most ways of assigning a value to the property don\u2019t do change propagation. If you want something dynamic, you have to ask for it. There\u2019s only two ways to do that: Besides the pay-for-play argument, there are two more reasons why {x:Static} doesn\u2019t do change propagation: So in our case, we could have used this notation: The bracker are mandatory for it to work and the XAML processor complains something about attached properties which does not exists: just ignore it.<\/p>\n As the property is named “Color”, I create a static event named “ColorChanged”<\/strong>. Each time the property value is updated, I raise it<\/strong> with an helper method. There is nothing more to do. public static event EventHandler ColorChanged; As this is a static property, there is no instance so null <\/em>is provided as a sender in the argument<\/strong>.<\/p>\n This solution is nice when you have only one property but can be fastidious when you have a lot of them because there is one event to define\/raise for each property. This is why the second solution is often prefered.<\/p>\n The generic event is what you are used to with normal property. Here too, as there is no instance, null is provided as a sender in the argument. public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged; This solution differs just a little from the non-static one : you can’t use string.empty as a property name to signal a change of every static properties values<\/strong>. If you do so you’ll, at the time I write this post, get an InvalidCastException<\/strong><\/em>: “Unable to cast object of type ‘PropertyRecord’ to type ‘ListenerList'”.<\/p>\n The demo – which can be found on my Dropbox folder<\/a> after registration if you want to give me space<\/a> \ud83d\ude42 – works fine and the rectangle color is updated on each button’s click. But when I tried to add a TextBlock binded to the same property under the rectangle, I noticed that the rectangle color was nor more updated.<\/p>\n After some investigation, I found out that it seems that only the first binding in the XAML seems to be working. The other are no more updated… Update:<\/strong> Sam told me that it will be fixed in the vNext.<\/p>\n Regards,<\/p>\n","protected":false},"excerpt":{"rendered":" There is two different ways to make a property bindable: implementing INotifyPropertyChanged is the most known solution, the other one is to create a event named PropertyNameChanged. In WPF 4.0 there is no way to…<\/p>\n","protected":false},"author":3,"featured_media":578,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[14],"_links":{"self":[{"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/posts\/570"}],"collection":[{"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/comments?post=570"}],"version-history":[{"count":22,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"predecessor-version":[{"id":605,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/posts\/570\/revisions\/605"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/media\/578"}],"wp:attachment":[{"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/tags?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}The theory<\/h3>\n
\n
It does not work with the x:Static extension \ud83d\ude41<\/h3>\n
\nIn the demo project, I have created an object named Repository which defines a static property named “Color”. I will use it to define a brush on a Rectangle so changes will be easy to watch.
\n<\/a><\/p>\n
\n public class Repository
\n {
\n static Repository() { UpdateColor(); }
\n static Random rnd = new Random((int)DateTime.Now.Ticks);
\n public static void UpdateColor()
\n {
\n Color = new Color()
\n {
\n A = 255,
\n R = (byte)rnd.Next(255),
\n G = (byte)rnd.Next(255),
\n B = (byte)rnd.Next(255),
\n };
\n }<\/p>\n
\n}
\n[\/csharp]<\/p>\n
\n[xml]<Application.Resources>
\n <local:Repository x:Key="Repository" \/>
\n<\/Application.Resources>
\n[\/xml]<\/p>\n
\n[xml]<Rectangle>
\n <Rectangle.Fill>
\n <SolidColorBrush Color="{Binding Color,Source={StaticResource Repository}}"\/>
\n <\/Rectangle.Fill>
\n<\/Rectangle>
\n[\/xml]<\/p>\nUPDATED: why the static extension does not work ?<\/h3>\n
\n<TextBlock Text=\u201dabc\u201d\/> assigns \u201cabc\u201d to the property, no change propagation (doesn\u2019t even make sense)
\n<TextBlock Text=\u201d{StaticResource C}\u201d\/> assigns a string to the property. No change propagation.
\n<TextBlock Text=\u201d{x:Static A.B}\u201d\/> assigns a string to the property. No change propagation.<\/p>\n
\n<TextBlock Text=\u201d{DynamicResource D}\u201d\/>: reacts to changes to the resource dictionary where D is defined
\n<TextBlock Text=\u201d{Binding Path=E}\u201d\/>: reacts to changes to the source property E<\/p>\n
\n1. Compatability: It didn\u2019t in 4.0.
\n2.Functionality: Some (most?) people still want the 4.0 behavior, with no change propagation, if only because it\u2019s cheaper. We\u2019d need a way to get that, {x:StaticWithNoChangePropagation} or the like.<\/p><\/blockquote>\n
\n[xml]<Rectangle>
\n <Rectangle.Fill>
\n <SolidColorBrush Color="{Binding (local:Repository.Color)}"\/>
\n <\/Rectangle.Fill>
\n<\/Rectangle>
\n[\/xml]<\/p>\nFirst solution: PropertyName<\/em>Changed event<\/h3>\n
\n[csharp]
\nprivate static Color _color;
\npublic static Color Color
\n{
\n get { return _color; }
\n set
\n {
\n if (_color == value) return;
\n _color = value;
\n RaiseColorChanged();
\n }
\n}<\/p>\n
\npublic static void RaiseColorChanged()
\n{
\n EventHandler handler = ColorChanged;
\n if (handler != null)
\n handler(null, EventArgs.Empty);
\n}
\n[\/csharp]<\/p>\nSecond solution: the generic <\/em>event<\/h3>\n
\nThe declaration of the event is usual. When you raise the event, you have to provide the name of the changed property to the event argument. <\/strong><\/p>\n
\n[csharp]private static Color _color;
\npublic static Color Color
\n{
\n get { return _color; }
\n set
\n {
\n if (_color == value) return;
\n _color = value;
\n RaiseStaticPropertyChanged("Color");
\n }
\n}<\/p>\n
\npublic static void RaiseStaticPropertyChanged(string propName)
\n{
\n EventHandler<PropertyChangedEventArgs> handler = StaticPropertyChanged;
\n if (handler != null)
\n handler(null, new PropertyChangedEventArgs(propName));
\n}
\n[\/csharp]<\/p>\nAdditional thoughts<\/h3>\n
\n<\/strong>
\nDid I missed something ? If yes, please tell it in the comments !<\/p>\n