Indeed, the 'canonical' way of doing this is to set up a DispatcherTimer
However, you can also do it with a storyboard and a fake converter like so:
<Window.Resources>
<local:AnythingToCurrentTimeConverter x:Key="conv"/>
<Storyboard x:Key="clockStory" Duration="0:0:2" RepeatBehavior="Forever">
<StringAnimationUsingKeyFrames
Storyboard.TargetName="clock"
Storyboard.TargetProperty="(Label.Tag)">
<DiscreteStringKeyFrame KeyTime="0:0:0" Value="Let's force binding" />
<DiscreteStringKeyFrame KeyTime="0:0:1" Value="..to change back and forth" />
</StringAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource clockStory}"/>
</EventTrigger>
</Window.Triggers>
<Grid>
<Label x:Name="clock" Content="{Binding ElementName=clock, Path=Tag, Converter={StaticResource conv}}"/>
</Grid>
..with converter being the following
public class AnythingToCurrentTimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return DateTime.Now.ToString("HH:mm:ss");
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Enjoy!
Without converter approach :
<TextBlock>
<TextBlock.Inlines>
<Run Text="This Test is : "/>
<Run x:Name="RunMyText" Text="{Binding myText}"/>
</TextBlock.Inlines>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Blue" />
<Style.Triggers>
<DataTrigger Binding="{Binding Text, ElementName=RunMyText}" Value="555">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Using converter :
public class MyTextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Int32.Parse(value.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML :
<TextBlock Text="{Binding myText, StringFormat= 'This Text is: {0}'}">
<TextBlock.Resources>
<local:MyTextConverter x:Key="MyTextCnvKey"/>
</TextBlock.Resources>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding myText, Converter={StaticResource MyTextCnvKey}}"
Value="555">
<Setter Property="Foreground"
Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>