c# - PropertyChangedEventHandler 为空

标签 c# .net wpf binding inotifypropertychanged

我有以下类将作为绑定(bind)源:

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 的 TimeTextBindingProp 属性之间的绑定(bind):

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 处理程序保持为空,即使在设置绑定(bind)并呈现 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>

最佳答案

除了设置 timeTextBinding 之外,您的代码是否以任何其他方式更新 timeTextBox.Text?也许您直接在代码隐藏中将 timeTextBox.Text 设置为某个值,或者您有一些其他绑定(bind)连接到它?

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

关于c# - PropertyChangedEventHandler 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14266007/

相关文章:

c# - 用 C# 和 Windows 编写事务文件?

c# - WPF - CommandBinding 到 F# 库中定义的命令不起作用

WPF LINQ 和 ObservableCollection

c# - 为什么 ASP.NET MVC4 在路由中添加尾部斜杠?

c# - 如何在 C# 中使用字符串和 double 获取二维数组的用户数据?

.net - 以编程方式检索 xml 文档注释

C# 无法显示具有正确颜色映射的 RAW16 灰度图像

.net - ML 仿函数可以在 .NET (C#/F#) 中完全编码吗?

c# - 为什么 WPF 窗口在 MainWindow 关闭后不自动处理和收集?

c# - 将图像从 Openfiledialog 保存到特定文件夹?