Hi,
If you use static and instance properties you must set instance property in callback method:
XAML MainWindow:
<Window x:Class="WpfApp1.Window88"
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:WpfApp88"
mc:Ignorable="d"
Title="Slider Binding" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<StackPanel DataContext="{Binding Obj}">
<TextBox Text="{Binding VolumeStartLeft}"/>
<Slider Value="{Binding VolumeStartLeft, Mode=TwoWay}"/>
</StackPanel>
</Window>
ViewModel:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Windows;
namespace WpfApp88
{
public class ViewModel
{
public c_delay_dp Obj { get; set; } = new c_delay_dp();
}
public class c_delay_dp : FrameworkElement, INotifyPropertyChanged
{
public float VolumeStartLeft
{
get { return (float)GetValue(VolumeStartLeftProperty); }
set
{
string stamp = DateTime.Now.ToString("HH:mm.fff");
Debug.WriteLine($"{stamp} - Setter VolumeStartLeft");
SetValue(VolumeStartLeftProperty, value);
}
}
public static readonly DependencyProperty VolumeStartLeftProperty = DependencyProperty.Register(
"VolumeStartLeft", typeof(float), typeof(c_delay_dp)
, new PropertyMetadata(1.0f,propCallBack)); // , VolumeStartLeftCallback
private static void propCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var dep = d as c_delay_dp;
if (dep == null) return;
dep.VolumeStartLeft = (float)e.NewValue;
dep.OnPropertyChanged(nameof(VolumeStartLeft));
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
Result: