PropertyChangedEventHandler is null

本文关键字:null is PropertyChangedEventHandler | 更新日期: 2023-09-27 17:51:19

我有以下类,这将是绑定源:

public class Timeline : Canvas, INotifyPropertyChanged
{
  public static readonly DependencyProperty TimeTextBindingPropProperty;
  public event PropertyChangedEventHandler PropertyChanged;
  private void OnPropertyChanged(string name)
  {
    if (PropertyChanged != null)
      PropertyChanged(this, new PropertyChangedEventArgs(name));
  }
  public double TimeTextBindingProp
  {
    get { return (double)GetValue(TimeTextBindingPropProperty); }
    set 
    {
      SetValue(TimeTextBindingPropProperty, value);
      OnPropertyChanged("TimeTextBindingProp");
    }
  }
  static Timeline()
  {
    TimeTextBindingPropProperty = DependencyProperty.Register("TimeTextBindingProp", typeof(double), typeof(Timeline));
  }
}

然后我在主窗口设置文本框的Text属性和timeline's TimeTextBindingProp属性之间的绑定:

private void InitTextBinding()
{
  timeTextBinding = new Binding();
  timeTextBinding.Mode = BindingMode.OneWay;
  timeTextBinding.Source = timeline;
  timeTextBinding.Path = new PropertyPath("TimeTextBindingProp");
  timeTextBinding.Converter = new TimeTextConverter();
  BindingOperations.SetBinding(this.timeTextBox, TextBox.TextProperty, timeTextBinding);
}
timeline

PropertyChanged处理程序在设置绑定并渲染timeline后仍然为空。我做错了什么?

编辑:

我在xaml中声明timeTextBox和timeline如下:

<StackPanel Orientation="Horizontal" Margin="0,10,0,5" HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBox Name="timeTextBox" Margin="0,0,20,0" VerticalAlignment="Center" HorizontalContentAlignment="Center" Height="20" 
                            FontFamily="Tahoma" FontSize="14" BorderBrush="White" Background="White"/>
    <Button Name="playButton" Style="{StaticResource buttonStyle}" Click="PlayButton_Click" Margin="0,0,5,0" HorizontalAlignment="Center" 
                        VerticalAlignment="Center">
                    Play
    </Button>
    <Button Name="stopButton" Style="{StaticResource buttonStyle}" Click="StopButton_Click" Margin="0,0,20,0" HorizontalAlignment="Center" 
                        VerticalAlignment="Center">
                    Stop
    </Button>
    <Slider Name="controlSlider" Height="Auto" Width="200" HorizontalAlignment="Center" VerticalAlignment="Center" IsEnabled="False">
      <Slider.ToolTip>
        <TextBlock Style="{StaticResource textStyleTextBlock}">Time Slider</TextBlock>
      </Slider.ToolTip>
    </Slider>
</StackPanel>
<ScrollViewer Name="scrollViewer" Margin="0,0,10,20" Height="500" HorizontalAlignment="Stretch" VerticalAlignment="Center" Focusable="False"
                      HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
  <StackPanel>
    <UI:FittingCanvas x:Name="containerCanvas">
      <timelinePanel:Timeline x:Name="timeline" TimeCursorEnabled="True" TimeMode="Minutes"  Canvas.Left="0" Canvas.Top="0" Canvas.ZIndex="0" Visibility="Hidden"
                                                CursorAnimBoundaryChanged="Timeline_CursorAnimBoundaryChanged" AnimationCompleted="Timeline_AnimationCompleted"/>
    </UI:FittingCanvas>
    <Canvas x:Name="waveFormCanvas" Height="80" Margin="0,10,0,0"/>
  </StackPanel>
</ScrollViewer>

PropertyChangedEventHandler is null

除了设置timeTextBinding,您的代码是否以其他方式更新timeTextBox.Text ?也许你可以设置timeTextBox。文本直接转换为代码隐藏中的某个值,或者您将其他绑定连接到它?

因为timeTextBinding只是单向,这样的更改不能写回TimeTextBindingProp,绑定将被简单地覆盖和删除(没有任何警告),timeline.PropertyChanged将重置为null