private void Init()
{
var tempBorder = win.FindName("border") as Border;
if (tempBorder != null)
{
border = tempBorder;
}
if (widthAnimation==null)
{
widthAnimation = new DoubleAnimation();
widthAnimation.From = shortWidth;
widthAnimation.To = longWidth;
widthAnimation.AutoReverse = true;
widthAnimation.RepeatBehavior = RepeatBehavior.Forever;
widthAnimation.Duration = TimeSpan.FromSeconds(3);
border.BeginAnimation(FrameworkElement.WidthProperty, widthAnimation);
}
}
private void CompositionTarget_Rendering(object? sender, EventArgs e)
{
TitleStr = border?.ActualWidth.ToString();
System.Diagnostics.Debug.WriteLine(TitleStr);
}
![]()
![]()
![]()
![]()
//xaml
<Window x:Class="WpfApp18.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:WpfApp18"
WindowState="Maximized"
mc:Ignorable="d"
Title="{Binding TitleStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Height="450" Width="800">
<Grid>
<Border x:Name="border"
Background="Red"
Height="500"
Width="{Binding BorderWidth,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
BorderBrush="Blue"
BorderThickness="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</Window>
//cs
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp18
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var vm = new MainVM(this);
this.DataContext = vm;
}
}
public class MainVM : INotifyPropertyChanged
{
private Window win;
private Border border;
private double longWidth = 0;
private double shortWidth = 100;
private DoubleAnimation widthAnimation;
public MainVM(Window winValue)
{
win= winValue;
if(win!=null)
{
win.Loaded += Win_Loaded;
CompositionTarget.Rendering += CompositionTarget_Rendering;
}
}
private void CompositionTarget_Rendering(object? sender, EventArgs e)
{
TitleStr = border?.ActualWidth.ToString();
System.Diagnostics.Debug.WriteLine(TitleStr);
}
private void Win_Loaded(object sender, RoutedEventArgs e)
{
longWidth = win.ActualWidth;
Init();
}
private void Init()
{
var tempBorder = win.FindName("border") as Border;
if (tempBorder != null)
{
border = tempBorder;
}
if (widthAnimation==null)
{
widthAnimation = new DoubleAnimation();
widthAnimation.From = shortWidth;
widthAnimation.To = longWidth;
widthAnimation.AutoReverse = true;
widthAnimation.RepeatBehavior = RepeatBehavior.Forever;
widthAnimation.Duration = TimeSpan.FromSeconds(3);
border.BeginAnimation(FrameworkElement.WidthProperty, widthAnimation);
}
}
public event PropertyChangedEventHandler? PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var handler = PropertyChanged;
if (handler != null)
{
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
private string titleStr;
public string TitleStr
{
get
{
return titleStr;
}
set
{
if(value!=titleStr)
{
titleStr = value;
OnPropertyChanged();
}
}
}
private double borderWidth;
public double BorderWidth
{
get
{
return borderWidth;
}
set
{
if (value != borderWidth)
{
borderWidth = value;
OnPropertyChanged();
}
}
}
}
}