Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding brush color to properties in ControlTemplate
    primarykey
    data
    text
    <p>I am trying to make a CustomControl that derives from Button, which simply shows as a colored rectangle. I want to have two properties on my control that I can set, which specify the normal color (ColdColor), and another color that will be used when the mouse is over the control (HotColor). </p> <p>I can't figure out how to get the binding set up between the brush color and the control properties. This is my code:</p> <p>Generic.xaml:</p> <pre><code>&lt;Style TargetType="{x:Type local:TestCustomControl}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type local:TestCustomControl}"&gt; &lt;Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Name="MyBorder"&gt; &lt;Border.Background&gt; &lt;!-- This works: --&gt; &lt;!--&lt;SolidColorBrush Color="Green" /&gt;--&gt; &lt;!-- This doesn't work: --&gt; &lt;SolidColorBrush Color="{TemplateBinding ColdColor}" /&gt; &lt;/Border.Background&gt; &lt;VisualStateManager.VisualStateGroups&gt; &lt;VisualStateGroup x:Name="CommonStates"&gt; &lt;VisualState x:Name="Normal"/&gt; &lt;VisualState x:Name="MouseOver"&gt; &lt;Storyboard&gt; &lt;!-- This works: --&gt; &lt;!--&lt;ColorAnimation Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="MyBorder" To="Red" Duration="0:0:0.2"/&gt;--&gt; &lt;!-- This doesn't work: --&gt; &lt;ColorAnimation Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="MyBorder" To="{TemplateBinding HotColor}" Duration="0:0:0.2"/&gt; &lt;/Storyboard&gt; &lt;/VisualState&gt; &lt;/VisualStateGroup&gt; &lt;/VisualStateManager.VisualStateGroups&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>TestCustomControl.cs:</p> <pre><code>public class TestCustomControl : Button { static TestCustomControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(TestCustomControl), new FrameworkPropertyMetadata(typeof(TestCustomControl))); } public Color HotColor { get { return (Color)GetValue(HotColorProperty); } set { SetValue(HotColorProperty, value); } } // Using a DependencyProperty as the backing store for HotColor. This enables animation, styling, binding, etc... public static readonly DependencyProperty HotColorProperty = DependencyProperty.Register("HotColor", typeof(Color), typeof(TestCustomControl), new UIPropertyMetadata(new Color())); public Color ColdColor { get { return (Color)GetValue(ColdColorProperty); } set { SetValue(ColdColorProperty, value); } } // Using a DependencyProperty as the backing store for ColdColor. This enables animation, styling, binding, etc... public static readonly DependencyProperty ColdColorProperty = DependencyProperty.Register("ColdColor", typeof(Color), typeof(TestCustomControl), new UIPropertyMetadata(new Color())); } </code></pre> <p>Usage in MainWindow.xaml:</p> <pre><code>&lt;my:TestCustomControl ColdColor="#FF0000AF" HotColor="#FFFF00AF"/&gt; </code></pre> <p>EDIT: To say that "doesn't work" means that the TestCustomControl is entirely transparent.</p>
    singulars
    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