{"id":1854,"date":"2015-06-16T06:20:39","date_gmt":"2015-06-16T06:20:39","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1854"},"modified":"2015-08-01T18:43:08","modified_gmt":"2015-08-01T18:43:08","slug":"how-to-properly-set-the-basedon-in-xaml-style","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/how-to-properly-set-the-basedon-in-xaml-style\/","title":{"rendered":"How to properly set the BasedOn in XAML style"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[1854]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>My <a href=\"http:\/\/stackoverflow.com\/questions\/24382614\/how-to-properly-set-the-basedon-in-xaml-style\">question<\/a> was:<\/p>\n<p>I have this as the style template:<\/p>\n<pre class=\"lang:default decode:true\">&lt;Style x:Key=\"myDogToggleButton1\" TargetType=\"ToggleButton\" BasedOn=\"{x:Null}\"&gt;\r\n    &lt;Setter Property=\"FocusVisualStyle\" Value=\"{x:Null}\"\/&gt;\r\n    &lt;Setter Property=\"Template\"&gt;\r\n        &lt;Setter.Value&gt;\r\n            &lt;ControlTemplate TargetType=\"ToggleButton\"&gt;\r\n                &lt;Grid&gt;\r\n                    &lt;Image Name=\"Normal\" Source=\"\/images\/dogs\/dog1.png\"\/&gt;\r\n                    &lt;Image Name=\"Pressed\" Source=\"\/images\/dogs\/dog3.png\" Visibility=\"Hidden\"\/&gt;\r\n                    &lt;Image Name=\"Disabled\" Source=\"images\/dogs\/dog5.png\" Visibility=\"Hidden\"\/&gt;\r\n                &lt;\/Grid&gt;\r\n                &lt;ControlTemplate.Triggers&gt;\r\n                    &lt;Trigger Property=\"IsPressed\" Value=\"True\"&gt;\r\n                        &lt;Setter TargetName=\"Normal\" Property=\"Visibility\" Value=\"Hidden\"\/&gt;\r\n                        &lt;Setter TargetName=\"Pressed\" Property=\"Visibility\" Value=\"Visible\"\/&gt;\r\n                    &lt;\/Trigger&gt;\r\n                    &lt;Trigger Property=\"IsEnabled\" Value=\"False\"&gt;\r\n                        &lt;Setter TargetName=\"Normal\" Property=\"Visibility\" Value=\"Hidden\"\/&gt;\r\n                        &lt;Setter TargetName=\"Disabled\" Property=\"Visibility\" Value=\"Visible\"\/&gt;\r\n                    &lt;\/Trigger&gt;\r\n                &lt;\/ControlTemplate.Triggers&gt;\r\n            &lt;\/ControlTemplate&gt;\r\n        &lt;\/Setter.Value&gt;\r\n    &lt;\/Setter&gt;\r\n&lt;\/Style&gt;<\/pre>\n<p>And now I want another one which is based on the upper one, and this doesn&#8217;t work:<\/p>\n<pre class=\"lang:default decode:true\">&lt;Style x:Key=\"myDogToggleButton2\" TargetType=\"ToggleButton\" BasedOn=\"{DynamicResource myDogToggleButton1}\"&gt;\r\n    &lt;Setter Property=\"Normal\" Value=\"\/images\/dogs\/dog2.png\" \/&gt;\r\n    &lt;Setter Property=\"Pressed\" Value=\"\/images\/dogs\/dog2.png\" \/&gt;\r\n    &lt;Setter Property=\"Disabled\" Value=\"\/images\/dogs\/dog2.png\" \/&gt;\r\n&lt;\/Style&gt;<\/pre>\n<p>The error message I get is:<\/p>\n<pre class=\"lang:default decode:true\">The member \"Pressed\" is not recognized or is not accessible.\r\nThe member \"Normal\" is not recognized or is not accessible.\r\nThe member \"Disabled\" is not recognized or is not accessible.<\/pre>\n<p>I suspect that my different style calling is wrong, so please point out the error.<\/p>\n<p>The answer, by user <a href=\"http:\/\/stackoverflow.com\/users\/3142505\/heena-patil\">Heena Patil<\/a>, was:<\/p>\n<blockquote><p>Try this<\/p>\n<p><strong>Resource<\/strong><\/p>\n<pre class=\"font-size:8 line-height:9 lang:default decode:true\">&lt;Window.Resources&gt;\r\n    &lt;Style x:Key=\"myDogToggleButton1\" TargetType=\"ToggleButton\"&gt;\r\n        &lt;Style.Resources&gt;\r\n            &lt;BitmapImage x:Key=\"Normal\" UriSource=\"Images\/darblue_tab.png\"\/&gt;\r\n            &lt;BitmapImage x:Key=\"Pressed\" UriSource=\"Images\/img-whitebg.png\" \/&gt;\r\n            &lt;BitmapImage x:Key=\"Disabled\" UriSource=\"Images\/img-greenbg.png\"\/&gt;\r\n        &lt;\/Style.Resources&gt;\r\n        &lt;Setter Property=\"FocusVisualStyle\" Value=\"{x:Null}\"\/&gt;\r\n        &lt;Setter Property=\"Template\"&gt;\r\n            &lt;Setter.Value&gt;\r\n                &lt;ControlTemplate TargetType=\"ToggleButton\"&gt;\r\n                    &lt;Grid&gt;\r\n                        &lt;Image Name=\"Normal\" Source=\"{DynamicResource ResourceKey=Normal}\" Stretch=\"Fill\"\/&gt;\r\n                        &lt;Image Name=\"Pressed\" Source=\"{DynamicResource ResourceKey=Pressed}\" Visibility=\"Hidden\"\/&gt;\r\n                        &lt;Image Name=\"Disabled\" Source=\"{DynamicResource ResourceKey=Disabled}\" Visibility=\"Hidden\"\/&gt;\r\n                    &lt;\/Grid&gt;\r\n                    &lt;ControlTemplate.Triggers&gt;\r\n                        &lt;Trigger Property=\"IsPressed\" Value=\"True\"&gt;\r\n                            &lt;Setter TargetName=\"Normal\" Property=\"Visibility\" Value=\"Hidden\"\/&gt;\r\n                            &lt;Setter TargetName=\"Pressed\" Property=\"Visibility\" Value=\"Visible\"\/&gt;\r\n                        &lt;\/Trigger&gt;\r\n                        &lt;Trigger Property=\"IsEnabled\" Value=\"False\"&gt;\r\n                            &lt;Setter TargetName=\"Normal\" Property=\"Visibility\" Value=\"Hidden\"\/&gt;\r\n                            &lt;Setter TargetName=\"Disabled\" Property=\"Visibility\" Value=\"Visible\"\/&gt;\r\n                        &lt;\/Trigger&gt;\r\n                    &lt;\/ControlTemplate.Triggers&gt;\r\n                &lt;\/ControlTemplate&gt;\r\n            &lt;\/Setter.Value&gt;\r\n        &lt;\/Setter&gt;\r\n    &lt;\/Style&gt;\r\n    &lt;Style x:Key=\"myDogToggleButton2\" TargetType=\"ToggleButton\" BasedOn=\"{StaticResource myDogToggleButton1}\"&gt;\r\n        &lt;Style.Resources&gt;\r\n            &lt;BitmapImage x:Key=\"Normal\" UriSource=\"Images\/img-darkbg.png\" \/&gt;\r\n            &lt;BitmapImage x:Key=\"Pressed\" UriSource=\"Images\/Screenshot_5.png\"\/&gt;\r\n            &lt;BitmapImage x:Key=\"Disabled\" UriSource=\"Images\/img-bluebg.png\"\/&gt;\r\n        &lt;\/Style.Resources&gt;\r\n    &lt;\/Style&gt;\r\n&lt;\/Window.Resources&gt;<\/pre>\n<p><strong>Xaml<\/strong><\/p>\n<pre class=\"font-size:8 line-height:9 lang:default decode:true\">&lt;Grid&gt;\r\n    &lt;ToggleButton HorizontalAlignment=\"Left\" Style=\"{StaticResource myDogToggleButton1}\"\/&gt;\r\n    &lt;ToggleButton  HorizontalAlignment=\"Right\" Style=\"{StaticResource myDogToggleButton2}\"\/&gt;\r\n&lt;\/Grid&gt;<\/pre>\n<p><strong>Update<\/strong> Using single style.<\/p>\n<pre class=\"font-size:8 line-height:9 lang:default decode:true  \">&lt;Grid&gt;\r\n    &lt;ToggleButton Height=\"300\" Width=\"300\" HorizontalAlignment=\"Left\" Style=\"{StaticResource myDogToggleButton1}\"\/&gt;\r\n    &lt;ToggleButton  Height=\"300\" Width=\"300\" Style=\"{StaticResource myDogToggleButton1}\" HorizontalAlignment=\"Right\"&gt;\r\n        &lt;ToggleButton.Resources&gt;\r\n            &lt;BitmapImage x:Key=\"Normal\" UriSource=\"Images\/img-darkbg.png\" \/&gt;\r\n            &lt;BitmapImage x:Key=\"Pressed\" UriSource=\"Images\/Screenshot_5.png\"\/&gt;\r\n            &lt;BitmapImage x:Key=\"Disabled\" UriSource=\"Images\/img-bluebg.png\"\/&gt;\r\n        &lt;\/ToggleButton.Resources&gt;\r\n    &lt;\/ToggleButton&gt;\r\n&lt;\/Grid&gt;<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1854","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1854"}],"version-history":[{"count":1,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1854\/revisions"}],"predecessor-version":[{"id":1855,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1854\/revisions\/1855"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}