Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble binding to a property of templatedparent to set the value inside of a style setter in wpf
    primarykey
    data
    text
    <p><strong>My problem:</strong></p> <p>I haven't been able to figure out how I inside the 'Setter.Value' field of a setter for a <em>property A</em> in a style targeting a particular control can do a binding to a <em>property B</em> of that particualr control. More specifically I want to use the Foreground brush value on a graphical element inside the visual tree of the Content property of a Button. this will ensure that the graphical element always has the foreground color set for this button control.</p> <p><strong>What I try to achive:</strong></p> <p>I'm working on a WPF-application where I have three button controls:</p> <ol> <li>DefaultButton</li> <li>SpecialButton</li> <li>ExtendedSpecialButton</li> </ol> <p>The <em>DefaultButton</em> is where I define the style of buttons in the application through a style with a ControlTemplate. </p> <p>The <em>SpecialButton</em> introduces a new property not supposed to be used for general buttons. This property will be represented by one visual state that I define through a style setter. Else from that it shall be identical in apperance to the DefaultButton.</p> <p>I define the style of this SpecialButton by basing it on the style of the DefaultButton. In this style there is no ControlTemplate only a MultiTrigger-response on the basis of a couple of property conditions setting av a couple of visual properties:</p> <pre><code>&lt;Style x:Key="SpecialButtonStyle" TargetType="{x:Type MyControls:SpecialButton}" BasedOn="{StaticResource DefaultButtonStyle}"&gt; &lt;Style.Triggers&gt; &lt;MultiTrigger&gt; &lt;MultiTrigger.Conditions&gt; &lt;Condition Property="IsActive" Value="false"/&gt; &lt;Condition Property="IsMouseOver" Value="false"/&gt; &lt;/MultiTrigger.Conditions&gt; &lt;Setter Property="BorderBrush" Value="{DynamicResource ButtonDisabledBorder}" /&gt; &lt;Setter Property="Background" Value="{DynamicResource ButtonDisabledBg}" /&gt; &lt;/MultiTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>All this worked great. </p> <p>The next step is also no problem: I wanted to base the <em>ExtendedSpecialButton</em> on the <em>SpecialButton</em> and set a default shape content inside the button.</p> <pre><code>&lt;Style x:Key="ExtendedSpecialButtonStyle" TargetType="{x:Type MyControls:ExtendedSpecialButton}" BasedOn="{StaticResource SpecialButtonStyle}"&gt; &lt;Setter Property="Content"&gt; &lt;Setter.Value&gt; &lt;Rectangle Fill="Black" Height="5" Width="15"&gt;&lt;/Rectangle&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>The original style of DefaultButton is still present - the added visual state responding to the IsActiveProperty of the SpecialButton is still with us - and the ExtendedSpecialButton also inherited the visual behaviour created by the MultiTrigger of the <em>SpecialButton</em>.</p> <p>I also successfully displayed a graphical element that this ExtendedSpecialButton should have.</p> <p>However I wanted the fill of this graphical element to use the Foreground color. This foreground color is originally styled in the DefaultButton and works just fine for the two first buttons.</p> <p>The code below is how I currently thought such a binding should be done. But this does not work:</p> <pre><code> &lt;Style x:Key="ExtendedSpecialButtonStyle" TargetType="{x:Type MyControls:ExtendedSpecialButton}" BasedOn="{StaticResource SpecialButtonStyle}"&gt; &lt;Setter Property="Content"&gt; &lt;Setter.Value&gt; &lt;Rectangle Fill="{Binding RelativeSource={RelativeSource TemplatedParent}}" Height="6" Width="20"&gt;&lt;/Rectangle&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>Does anyone know what I could do to set up the binding so that it does what I intended it to do?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload