How does a ControlTemplate handle the datacontext?
Using the the follow Template
<ControlTemplate x:Key="ToolbarButtonHover" TargetType="Button">
<Grid Name="backgroundGrid">
<Image Source="{DynamicResource ResourceKey=Img}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"></Image>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=DataContext.ToolSelected, RelativeSource={RelativeSource TemplatedParent}}" Value="Unlink">
<Setter TargetName="backgroundGrid" Property="Background" Value="Red" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
with the control
<Button Content="Button"
Template="{StaticResource ResourceKey=ToolbarButtonHover}"
Height="24" Width="24" Background="Red">
<Button.Resources>
<ImageSource x:Key="Img">Resources/Icons/toolSelect.png</ImageSource>
</Button.Resources>
</Button>
But this does not make the background red. I have verifyed that the value of the ToolbarViewModel Property ToolSelected
is in fact Unlink by having a <Label Content="{Binding ToolSelected}"/>
next to the controll. So i believe the problem is that the template does not use the correct DataContext
, but I'm not sure of this. That's why i ask you for help.
The Control lies in a custom usercontrol, and the ToolbarViewModel is set as context for all of it, like so.
<UserControl.DataContext>
<local:ToolboxView/>
</UserControl.DataContext>
Try removing RelativeSource
from DataTrigger.Binding
then it should work in current DataContext
:
<DataTrigger Binding="{Binding ToolSelected}" Value="Unlink">
<Setter TargetName="backgroundGrid" Property="Background" Value="Red" />
</DataTrigger>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With