C
C#16mo ago
lucy

❔ wpf styling/templating with custom dependency properties?

is there a way to use custom dependency properties with styles and templates? for example if I want a property, MiddleColour, on a template for a slider, that changes the background colour of the middle bit of the slider and I want to be able to change that colour using a style. Is that possible? Continuing on, would it also be possible to pass a property to the style, eg GradientColour, which is then used and passed to the MiddleColour in the template? Thanks
6 Replies
JakenVeina
JakenVeina16mo ago
you can customize the visuals of a control by providing it a custom ControlTemplate and in theory, I think you could define your MiddleColour property as an attached property, and be able to bind to that from within the template not entirely sure what you're asking with the second question
lucy
lucyOP16mo ago
tyty with the second question, so the MiddleColour is a brush, but i'd like a style that gives it a gradient brush that goes from #000000 to a given colour GradientColour
JakenVeina
JakenVeina16mo ago
well, that is a thing you can do with brushes
lucy
lucyOP16mo ago
yes, but i'm asking specifically is there a way to get it so you have the compnent, give it a style, give it a GradientColour, and the style handles translating GradientColour into a gradient brush rather than having to pass in a unique brush for each colour you want to use as a gradient for example
<Slider Style="{StaticResource ColourSliderStyle}" GradientColour="#ff0000"/>

<!--in the resources file-->

<Style x:Key=ColourSliderStyle TargetType="x:Type Slider">
<Setter Property="MiddleColour">
<Setter.Value>
<GradientBrush>
<GradientStop Color="#000000" Offset="0"/>
<GradientStop Color="{[passed GradientColour]}" Offset="1"/>
</GradientBrush>
</Setter.Value>
</Setter>
</Style>
<Slider Style="{StaticResource ColourSliderStyle}" GradientColour="#ff0000"/>

<!--in the resources file-->

<Style x:Key=ColourSliderStyle TargetType="x:Type Slider">
<Setter Property="MiddleColour">
<Setter.Value>
<GradientBrush>
<GradientStop Color="#000000" Offset="0"/>
<GradientStop Color="{[passed GradientColour]}" Offset="1"/>
</GradientBrush>
</Setter.Value>
</Setter>
</Style>
JakenVeina
JakenVeina16mo ago
yeah, that should be doable, assuming what I said earlier is doable with passed being an attached property and with that being inside a ControlTemplate you'd have that be a TemplateBinding IIRC
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server