当各个属性绑定到ScrollBar时,PropertyChangedCallback不会触发

时间:2015-03-09 07:25:11

标签: 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(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); }
        }

此控件用于简单表格;相关的XAML如下:

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

&#34; Text&#34; TextBox的属性和&#34; Angle&#34;我的控制权属于&#34;价值&#34; ScrollBar的属性使用以下绑定:

"{Binding ElementName=scrollBarAngle, Path=Value}"

当我滚动ScrollBar时,Text字段按预期更新,但Angle不会改变 - OnAngleChanged回调未被调用!

但是,如果我直接更改ScrollBar的ValueChanged事件中的Angle属性,一切正常 - 属性已更改并且相应的回调被触发:

        private void scrollBarAngle_ValueChanged( object sender, RoutedPropertyChangedEventArgs<double> e )
        {
        compassControl.Angle = e.NewValue;
        }

请帮助解决此问题!

谢谢你, --Alex

1 个答案:

答案 0 :(得分:0)

我的道歉 - 问题不在代码中,而是在环境中!我有几个VS2013打开的副本,该项目在其中两个开放。无论如何,在阅读了Clemens评论表明我的问题无法重现之后,我关闭了VS的所有实例,然后启动了新实例,打开了项目 - 一切正常!

谢谢!