【问题标题】:PropertyChangedEventHandler is nullPropertyChangedEventHandler 为空
【发布时间】:2012-12-25 07:37:24
【问题描述】:

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

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);
}

timelinePropertyChanged 处理程序即使在设置绑定并呈现timeline 后仍为空。我做错了什么?

编辑

我在xaml中声明timeTextBox和时间线如下:

<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>

【问题讨论】:

  • 为什么你的绑定模式是单向的?使用文本框应该是双向的。
  • @slfan 可能想要支持复制。无法复制文本块。
  • 仅供展示,不可编辑
  • 那么TextBlock更快
  • 输出窗口中有任何消息吗? (绑定和转换错误只显示在那里)

标签: c# .net wpf binding inotifypropertychanged


【解决方案1】:

除了设置timeTextBinding 之外,您的代码是否更新timeTextBox.Text?也许您直接在代码隐藏中将 timeTextBox.Text 设置为某个值,或者您有其他绑定?

因为timeTextBinding只是OneWay,所以这样的改变是无法写回TimeTextBindingProp的,绑定只会被简单的覆盖和删除(没有任何警告),timeline.PropertyChanged会重置为null

【讨论】:

  • 感谢您的回答。在设置绑定后,我将 timeTextBox.text 设置为某个字符串,这就是问题的原因。在我删除它之后,绑定开始工作。这是否意味着在设置绑定后不应设置任何属性值,否则绑定将被取消?如果是这样的话,这对我来说很奇怪。根据我的逻辑,绑定应该忽略集合或立即用绑定值覆盖集合。这是因为绑定的优先级低于本地修改吗?再次感谢您的回答,因为它解决了问题。
  • 是的,在 OneWay 绑定上,它就是这样工作的。如果要手动更改该值,则应设置TimeTextBindingProp 而不是timeTextBox.Text 以通过绑定系统“以正确的方式”推送值。在 TwoWay 绑定上,您可以手动更改任一属性,而另一个将相应更改(如预期的那样)。
猜你喜欢
  • 2011-04-05
  • 2012-02-15
  • 2011-04-13
  • 1970-01-01
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多