0

I want to create a pure xaml digital clock (without background code)

Please refer to the details

Create a digital clock using only xaml code in wpf

But there are some problems when binding properties, it doesn't work.

I used the ObjectDataProvider to bind the date obtained by DateTime.AddSeconds() to the TextBlock, which works when I manually modify it, but it doesn't work when I use animation changes.

<Window x:Class="WpfApp9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp9"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>

        <ObjectDataProvider x:Key="AddSeconds" MethodName="AddSeconds" ObjectInstance="{x:Static sys:DateTime.Now}">
            <ObjectDataProvider.MethodParameters>
                <sys:Double>0</sys:Double>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

    </Window.Resources>

    <Grid>

        <!--Updates can be triggered by modifying the text property of temp-->
        <TextBox x:Name="temp"
    Text="{Binding ElementName=tbx,Mode=OneWayToSource,Path=Width,UpdateSourceTrigger=PropertyChanged,FallbackValue=0}" Margin="0,-100,0,0" Width="100" Height="40"/>


        <TextBox x:Name="tbx" Height="40" 
        Text="{Binding ElementName=tbx,Path=Width}"
        Width="{Binding Source={StaticResource AddSeconds},Path=MethodParameters[0],BindsDirectlyToSource=True,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay,FallbackValue=0}"  />

        <TextBlock Margin="0,100,0,0" 
                Text="{Binding Source={StaticResource AddSeconds},Mode=OneWay}"/>

    </Grid>
    <Window.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <!--This animation that modifies the Width property of tbx does not work.-->

                    <DoubleAnimation 
                            Storyboard.TargetName="tbx"
                            Storyboard.TargetProperty="Width"      
                            From="0"
                            To="59"
                            Duration = "0:0:59"
                            />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>
</Window>
  • I assume this is just an academic exercise, and you don't actually _need_ to implement a strictly XAML-only clock. I don't think you're going to find this possible with that approach. `ObjectDataProvider` is evaluated with the instance of the provider is created, one time. You would need to call the `Refresh()` method on the object to force an update, and I'm not aware of any way to do that from XAML. Fortunately, there are much better ways to implement a clock in WPF. :) – Peter Duniho Mar 31 '19 at 05:49
  • @PeterDuniho Thank you for your answer, I am giving up this question now :) – Blue Zhang Apr 01 '19 at 12:53

0 Answers0