我有以下的類,這將是綁定源: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);
}
PropertyChanged
timeline
的處理程序仍然爲空,即使已設置綁定並且已呈現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>
爲什麼你的bindingmode單向?用文本框它應該是雙向的。 – slfan
@slfan可能需要支持副本。無法複製文本塊。 – Paparazzi
它只是用於顯示不用於編輯 –