2013-01-10 66 views
1

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

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

PropertyChangedtimeline的处理程序仍然为空,即使已设置绑定并且已呈现timeline。我究竟做错了什么?

编辑

我宣布和文字框时间轴XAML如下:

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

为什么你的bindingmode单向?用文本框它应该是双向的。 – slfan

+0

@slfan可能需要支持副本。无法复制文本块。 – Paparazzi

+0

它只是用于显示不用于编辑 –

回答

2

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

因为timeTextBinding仅单向,这种改变不能被写回到TimeTextBindingProp,并结合将简单地被覆盖并删除(无任何警告),并timeline.PropertyChanged将被重置为null

+0

感谢您的答案。绑定设置后,我将timeTextBox.text设置为某个字符串,这是问题的原因。我删除这个后,绑定开始工作。这是否意味着在绑定设置后,什么都不应该设置属性值,否则绑定将被取消?如果是这样,这对我来说很奇怪。根据我的逻辑,绑定应忽略该集合或立即用绑定值覆盖集合。这是因为绑定的优先级低于本地修改吗?感谢解决问题的答案。 –

+0

是的,在_OneWay_绑定这就是它的工作原理。如果您想手动更改该值,则应该设置'TimeTextBindingProp'而不是'timeTextBox.Text',以通过绑定系统推送值“正确的方式”。在_TwoWay_绑定上,您可以手动更改任一属性,而另一个会相应更改(如预期的那样)。 – Sphinxxx

2

TimeTextBindingProp是一个依赖属性,有一个NotificationChange机制,构筑成的基类。你自己的OnPropertyChanged是多余的,显然它不被使用。

绑定仍然应该工作,虽然。

+0

它无法使用或不使用OnPropertyChanged调用。 TimeTextConverter的Convert方法仅在调用SetBinding后调用 –

+0

您可能必须指定UpdateTrigger(默认可能是LostFocus)。 –

+0

它也不起作用,如果我直接指定依赖属性而不是CLR包装的Binding.Path为“timeTextBinding.Path = new PropertyPath(Timeline.TimeTextBindingPropProperty);” –

相关问题