C# PropertyChangedEventHandler为空
我有以下类作为绑定源:C# PropertyChangedEventHandler为空,c#,.net,wpf,binding,inotifypropertychanged,C#,.net,Wpf,Binding,Inotifypropertychanged,我有以下类作为绑定源: public class Timeline : Canvas, INotifyPropertyChanged { public static readonly DependencyProperty TimeTextBindingPropProperty; public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string name)
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
属性之间的绑定:
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中声明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设置为某个值,或者您连接了其他绑定
因为timeTextBinding只是单向的,这样的更改不能写回
TimeTextBindingProp
,绑定将被覆盖和删除(没有任何警告),并且timeline.PropertyChanged
将重置为null
为什么您的绑定模式是单向的?对于文本框,它应该是双向的。@slfan可能希望支持复制。无法复制textblock。它仅用于显示,不用于编辑。那么textblock是输出窗口中的fasterAny消息吗?(绑定和转换错误仅在此处显示)感谢您的回答。设置绑定后,我将timeTextBox.text设置为一些字符串,这就是问题的原因。在我删除这个之后,绑定开始工作。这是否意味着在设置绑定后,不应设置任何属性值,否则将取消绑定?如果是这样的话,我觉得这很奇怪。根据我的逻辑,绑定应该忽略集合或立即用绑定值覆盖集合。这是因为绑定的优先级低于本地修改吗?再次感谢您的回答,因为它解决了这个问题。是的,在单向绑定上,它就是这样工作的。如果要手动更改值,应设置TimeTextBindingProp
,而不是timeTextBox.Text
,以通过绑定系统“正确”推送值。在双向绑定上,您可以手动更改其中一个属性,而另一个属性将相应地更改(如预期的那样)。