In WPF, why does the Rectangle.Fill property not seem to work when using a TemplateBinding?
-
21-08-2019 - |
Question
I can't figure out why this XAML code does not work. When using a TemplateBinding (see below), the background color is not set. But when I use a normal color string (i.e. "Red"), it works fine.
<ControlTemplate x:Key="InstanceButtonTemplate" TargetType="{x:Type Control}">
<Grid>
<Rectangle>
<Rectangle.Fill>
<SolidColorBrush Color="{TemplateBinding Background}"></SolidColorBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</ControlTemplate>
Yet, when I use a TemplateBinding in this way, it works Fine...
<ControlTemplate x:Key="InstanceButtonTemplate" TargetType="{x:Type Control}">
<Grid>
<Rectangle Fill="{TemplateBinding Background}"></Rectangle>
</Grid>
</ControlTemplate>
Any ideas?
Edit: to clarify, I intend to expand this to rather use a gradient brush, that's why I need to be able to assign to the Rectangle.Fill property using XAML instead of a plain string.
Solution
That is because Color has a different type then Background
Background is a Brush, Color is a.. well Color.. You can use a IValueConverter to convert your brush to a color..
HTH
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow