I am trying to change the background property in DataTemplate according to AttachedProperty value of the element, I don't get any binding errors but the Background field stay with its default value. What is the prorblem?
View:
<ContentControl common:IsOpen={Binding IsOpenValue} ContentTemplate="{StaticResource MyTemplate}"/>
DataTemplate:
<DataTemplate x:Key="MyTemplate">
<Border Width="20" Height="20" Name="TheName"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding (common:IsOpen), RelativeSource={RelativeSource Self}}" Value=common:IsOpenEnum.Open>
<Setter Property="Background" TargetName="TheName" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding (common:IsOpen), RelativeSource={RelativeSource Self}}" Value=common:IsOpenEnum.Closed>
<Setter Property="Background" TargetName="TheName" Value="Green"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Several issues here:
You have to use the full syntax when binding to attached properties. Something like:
<DataTrigger Binding="{Binding Path=(common:IsOpen), Rela....
Next, using RelativeSource={RelativeSource Self}
will probably give you the ContentPresenter
that is in the control template of the ContentControl
, when you are actually looking for the property on the ContentControl
. Instead, use
....RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}...
Lastly, for brevity, drop common:IsOpenEnum.
from the value. Use just the value of the enum:
....Value="Open">
[I believe the lack of quotations for the value is a glitch here and that they do appear in real code]