【WPF】绑定静态属性

  • Post author:
  • Post category:其他




1. XAML

<Window x:Class="AppDemo.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:system="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:AppDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="800">
    <Grid Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0" Text="Path=(system:DateTime.Now)" />
        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=(system:DateTime.Now), UpdateSourceTrigger=PropertyChanged}" />

        <TextBlock Grid.Row="1" Grid.Column="0" Text="Source={x:Static system:DateTime.Now}" />
        <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Source={x:Static system:DateTime.Now}}" />

        <TextBlock Grid.Row="2" Grid.Column="0" Text="Source={x:Static system:DateTime.Now}, Path=." />
        <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Source={x:Static system:DateTime.Now}, Path=.}" />

        <TextBlock Grid.Row="3" Grid.Column="0" Text="Source={x:Static local:MainWindow.CurrentTime}" />
        <TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding Source={x:Static local:MainWindow.CurrentTime}}" />

        <TextBlock Grid.Row="4" Grid.Column="0" Text="Source={x:Static local:MainWindow.CurrentTime}, Path=." />
        <TextBlock Grid.Row="4" Grid.Column="1" Text="{Binding Source={x:Static local:MainWindow.CurrentTime}, Path=.}" />

        <TextBlock Grid.Row="5" Grid.Column="0" Text="Source={x:Static local:MainWindow.CurrentTime}, Path=., Mode=TwoWay" />
        <TextBlock Grid.Row="5" Grid.Column="1" Text="{Binding Source={x:Static local:MainWindow.CurrentTime}, Path=., Mode=TwoWay}" />

        <TextBlock Grid.Row="6" Grid.Column="0" Text="Path=CurrentTime, Mode=TwoWay" />
        <TextBlock Grid.Row="6" Grid.Column="1" Text="{Binding Path=CurrentTime}" />
    </Grid>
</Window>



2. CS

using System;
using System.ComponentModel;
using System.Windows;

namespace AppDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();

            this.DataContext = this;
            var timer = new System.Timers.Timer(1000);
            timer.AutoReset = true;
            timer.Elapsed += (s, e) => CurrentTime = DateTime.Now;
            timer.Start();
        }

        public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

        private static DateTime currentTime = DateTime.Now;

        public static DateTime CurrentTime
        {
            get => currentTime;
            set
            {
                lock (nameof(MainWindow))
                {
                    if (currentTime == value)
                    {
                        return;
                    }
                    currentTime = value;
                    if (StaticPropertyChanged != null)
                    {
                        StaticPropertyChanged.Invoke(null, new PropertyChangedEventArgs(nameof(CurrentTime)));
                    }
                }
            }
        }
    }
}



3. 效果

在这里插入图片描述



版权声明:本文为zhy29563原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。