WPF - Binding a color resource to the data object within a DataTemplate
I have a DataTemplate
and a SolidColorBrush
in the DataTemplate.Resources
section.
I want to bind the color to a property of the same data object that the DataTemplate
itself is bound to.
However, this does not work. The brush is ignored. Why?
Here is the simplified code:
<DataTemplate DataType="{x:Type data:MyData}" x:Name="dtData">
<DataTemplate.Resources>
<SolidColorBrush x:Key="bg" Color="{Binding Path=Color, Converter={StaticResource colorConverter}" />
</DataTe开发者_如何学JAVAmplate.Resources>
<Border CornerRadius="15"
Background="{StaticResource bg}"
Margin="0"
Opacity="0.5"
Focusable="True">
</DataTemplate>
I understand I could set this directly also but I need the color to be a resource.
"Works on My Machine" :) I have one theory. You binding is working, you border has no content so it's only consist of a border itself, but you setting background property not the BorderBrush, so actualy you background has no area, also you don't set BorderThickness so actually you have border with 0 width and 0 height. So set BorderThickness, Width or Height.
Alternatively, you can simplify with below.
<DataTemplate DataType="{x:Type data:MyData}" x:Name="dtData">
<Border CornerRadius="15"
Background="{Binding Path=Color, Converter={StaticResource colorConverter}}"
Margin="0"
Opacity="0.5"
Focusable="True">
</DataTemplate>
精彩评论