C# 当相应的属性绑定到滚动条时,PropertyChangedCallback不触发
我有一个非常简单的UserControl,我想用DependencyProperty扩展它。控制的相关代码如下:C# 当相应的属性绑定到滚动条时,PropertyChangedCallback不触发,c#,wpf,dependency-properties,propertychanged,data-binding,C#,Wpf,Dependency Properties,Propertychanged,Data Binding,我有一个非常简单的UserControl,我想用DependencyProperty扩展它。控制的相关代码如下: public partial class CompassControl : UserControl { public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(Double), typeof(Com
public partial class CompassControl : UserControl
{
public static readonly DependencyProperty AngleProperty =
DependencyProperty.Register("Angle", typeof(Double), typeof(CompassControl),
new FrameworkPropertyMetadata( 0.0, FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(OnAngleChanged)));
private static void OnAngleChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
((CompassControl)target).SetImageAngle((Double)e.NewValue);
}
public CompassControl( )
{
InitializeComponent( );
}
public Double Angle
{
get { return (Double)GetValue(AngleProperty); }
set { SetValue(AngleProperty, value); }
}
<DockPanel DockPanel.Dock="Bottom">
<DockPanel>
<TextBlock DockPanel.Dock="Left"
TextAlignment="Center" FontWeight="Bold" FontSize="12"
Padding="0,4,0,0" HorizontalAlignment="Left"
Height="22" Width="60" Margin="10,0,0,0"
Text="{Binding ElementName=scrollBarAngle, Path=Value}">
</TextBlock>
<ScrollBar DockPanel.Dock="Left" Name="scrollBarAngle" Orientation="Horizontal"
Height="22" Margin="10,0"
Maximum="360.0" Minimum="0.0" SmallChange="1.0" Value="0.0"
ValueChanged="scrollBarAngle_ValueChanged" />
</DockPanel>
</DockPanel>
<ctl:CompassControl DockPanel.Dock="Top" Name="compassControl"
Margin="5" Width="Auto" Height="Auto"
Angle="{Binding ElementName=scrollBarAngle, Path=Value}"
/>
</DockPanel>
此控件正在一个简单表单上使用;相关XAML如下所示:
public partial class CompassControl : UserControl
{
public static readonly DependencyProperty AngleProperty =
DependencyProperty.Register("Angle", typeof(Double), typeof(CompassControl),
new FrameworkPropertyMetadata( 0.0, FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(OnAngleChanged)));
private static void OnAngleChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
((CompassControl)target).SetImageAngle((Double)e.NewValue);
}
public CompassControl( )
{
InitializeComponent( );
}
public Double Angle
{
get { return (Double)GetValue(AngleProperty); }
set { SetValue(AngleProperty, value); }
}
<DockPanel DockPanel.Dock="Bottom">
<DockPanel>
<TextBlock DockPanel.Dock="Left"
TextAlignment="Center" FontWeight="Bold" FontSize="12"
Padding="0,4,0,0" HorizontalAlignment="Left"
Height="22" Width="60" Margin="10,0,0,0"
Text="{Binding ElementName=scrollBarAngle, Path=Value}">
</TextBlock>
<ScrollBar DockPanel.Dock="Left" Name="scrollBarAngle" Orientation="Horizontal"
Height="22" Margin="10,0"
Maximum="360.0" Minimum="0.0" SmallChange="1.0" Value="0.0"
ValueChanged="scrollBarAngle_ValueChanged" />
</DockPanel>
</DockPanel>
<ctl:CompassControl DockPanel.Dock="Top" Name="compassControl"
Margin="5" Width="Auto" Height="Auto"
Angle="{Binding ElementName=scrollBarAngle, Path=Value}"
/>
</DockPanel>
当我滚动滚动条时,文本字段会按预期更新,但角度不会改变-不会调用OnAngleChanged回调
但是,如果我在滚动条的ValueChanged事件中直接更改Angle属性,一切正常-该属性已更改,相应的回调被触发:
private void scrollBarAngle_ValueChanged( object sender, RoutedPropertyChangedEventArgs<double> e )
{
compassControl.Angle = e.NewValue;
}
private void scrollBarAngle_值已更改(对象发送方,RoutedPropertyChangedEventArgs e)
{
compassControl.Angle=e.NewValue;
}
请帮助解决此问题
谢谢,,
--Alex我的道歉-问题不在代码中,而是在环境中!我打开了几份VS2013,项目在其中两份中打开。无论如何,在阅读了Clemens的评论,指出我的问题是不可复制的之后,我关闭了VS的所有实例,然后启动了新实例,打开了项目——一切都很好
谢谢大家! 你可以使用触发器,请看你的代码是否真的有效。我添加了带有测试输出的
SetImageAngle
方法,并按预期调用它。您在此处显示的代码无法再现此问题。克莱门斯,您使用我的示例代码时是否使用了事件方法scrollBarAngle\u ValueChanged(…)?如果我使用它并强制更新compassControl.Angle,一切正常。如果我注释掉这一行,则不会向compassControl传播任何更新。我可以上传整个项目供你审查-它是相当小的。