2013-01-10 63 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);」 –

相關問題