I want to style a button depending on a ViewModel property, which is not part of the button.
So I created the following
The Button XAML:
<Button Content="DisableButton"
Height="27"
HorizontalAlignment="Left"
Margin="95,197,0,0"
VerticalAlignment="Top"
Width="120"
Style="{DynamicResource ButtonOffline}"
Click="ButtonDisableClick">
</Button>
The style (the Trigger with Property does not work. also tried DataTrigger)
<Grid.Resources>
<Style x:Key="ButtonOffline" TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsOnline}" Value="false">
<Setter Property="Background" Value="#FF808080"></Setter>
<Setter Property="BorderBrush" Value="Red"></Setter>
<Setter Property="BorderThickness" Value="5"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding IsOnline}" Value="true">
<Setter Property="Background" Value="#FF990000"></Setter>
<Setter Property="BorderBrush" Value="Blue"></Setter>
<Setter Property="BorderThickness" Value="2"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Resources>
The property
private bool isOnline;
public bool IsOnline
{
get
{
return isOnline;
}
set
{
isOnline = value;
NotifyPropertyChanged("IsOnline");
}
}
And my click handler
private void ButtonDisableClick(object sender, RoutedEventArgs e)
{
var isOnline = (this.DataContext as ComboBoxSampleViewModel).IsOnline;
(this.DataContext as ComboBoxSampleViewModel).IsOnline = !isOnline;
}
Now if I click the button, something like a storyboard begins and the buttons starts to change colors like an animation.
Whats wrong with the code?
Update Reproduced in a small sample which just has the button features posted here. Nothing (except the basic INotifyPropertyChanged implementation) else which is not posted here.