I'm having a problem with a piece of code that's starting to drive me out of my mind. I have a base class in WPF with a declared DependencyProperty:
public partial class AudioTimeControlBase : UserControl
{
public static readonly DependencyProperty PlaybackPositionProperty = DependencyProperty.RegisterAttached(
"PlaybackPosition", typeof(TimeSpan), typeof(AudioTimeControlBase),
new FrameworkPropertyMetadata(TimeSpan.Zero,
FrameworkPropertyMetadataOptions.Inherits |
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
OnPlaybackPositionChanged, CoercePlaybackPosition));
public TimeSpan PlaybackPosition
{
get { return (TimeSpan)this.GetValue(PlaybackPositionProperty); }
set { this.SetValue(PlaybackPositionProperty, value); }
}
private static void OnPlaybackPositionChanged(object sender, DependencyPropertyChangedEventArgs e)
{
AudioTimeControlBase control = sender as AudioTimeControlBase;
if (control != null)
control.OnPlaybackPositionChanged(e);
}
protected virtual void OnPlaybackPositionChanged(DependencyPropertyChangedEventArgs e)
{
}
private static object CoercePlaybackPosition(DependencyObject d, object value)
{
TimeSpan t = (TimeSpan)value;
if(t < TimeSpan.Zero)
return TimeSpan.Zero;
return t;
}
}
Then a derived control:
public partial class WaveViewerControl : AudioTimeControlBase
{
public WaveViewerControl()
{
InitializeComponent();
}
void playbackControl_PositionChanged(object sender, EventArgs e)
{
PlaybackPosition = ConvertFromPosition(playbackControl.Position);
}
}
I've taken out a fair amount of stuff but I really think I've isolated the offending bits to this. The problem is that when playbackControl_PositionChanged happens (as a result of user action), it breaks the (previously working) binding to the PlaybackPositionProperty and the control receives no subsequent updates. If anyone has any thoughts or leads at all I would be eternally grateful. Thanks!
UPDATE: I realize I left some potentially critical information out of this - this control is not being directly represented in my XMAL but is being programmatically added to a parent control. However, per the "Inhert" option, the bindings of that parent are being correctly inherited. It's not until the SetValue call that they are apparently overridden. Does this suggest that two-way binding is not supported by inherited properties?