I'm using some buttons with templated content
e.g.
button.Template = (ControlTemplate)FindResource("StatusEmpty");
In the same code behind file is a (global) field named currentNumber
private int currentNumber = 1;
public int CurrentNumber
{
get
{
return currentNumber;
}
}
These templates are defined in a merged RessourceDictionary
<ControlTemplate x:Key="BinStatusEmpty" TargetType="Button">
<Border x:Name="border" BorderBrush="LightGray" CornerRadius="8,8,8,8" Margin="3" >
<Border.Background>
<LinearGradientBrush>
<GradientStop Offset="0" Color="white"></GradientStop>
<GradientStop Offset="1" Color="LightGray"></GradientStop>
</LinearGradientBrush>
</Border.Background>
<ContentPresenter Content = "{TemplateBinding Content}"
HorizontalAlignment = "Center" VerticalAlignment = "Center"
TextBlock.FontSize="40"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="Blue" TargetName="border" />
<Setter Property="BorderThickness" Value="4" TargetName="border" />
</Trigger>
<DataTrigger Binding="{Binding currentNumber, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Value="{TemplateBinding Content}" >
<Setter Property="BorderBrush" Value="Blue" TargetName="border" />
<Setter Property="BorderThickness" Value="4" TargetName="border" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
What I Want is a data trigger which changes the border defined in the template dynamic based on the current c# variable.
There are currently two problems: 1) The binding
{Binding currentNumber, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}
doesn't find my variable
BindingExpression path error: 'currentNumber' property not found on 'object' ''MainWindow' (Name='xMainWindow')'. BindingExpression:Path=currentNumber; DataItem='MainWindow' (Name='xMainWindow'); target element is 'BinButton' (Name=''); target property is 'NoTarget' (type 'Object')
2) I can't bind
{TemplateBinding Content}
as value of the datatrigger.
Thanks a lot
René