可以控制绑定另一个Usercontrol

时间:2013-11-12 05:26:38

标签: c# wpf user-controls

我有一个带有DependencyProperty SliderZoom的UserControl,它是一个Slider类型。在我的窗口中,我有一个控件谁的名字是SliderZoom,我想在它上面使用绑定。

这甚至可能吗?如果不是,我怎么能达到这个目的呢?

窗口代码

<local:NewSlider
  SliderZoom="{Binding SliderZoom}">
</local:NewSlider>

 <Slider x:Name="SliderZoom"
                Width="150"
                Interval="1"
                Maximum="10"
                Minimum="0" />

UserControl NewSlider代码

    public Slider SliderZoom
    {
        get { return (Slider)GetValue(ZoomSliderProperty); }
        set { SetValue(ZoomSliderProperty, value); }
    }

    public static readonly DependencyProperty ZoomSliderProperty =
    DependencyProperty.Register("SliderZoom", typeof(Slider), typeof(NewTimeSlider),
    new PropertyMetadata(OnZoomSliderChanged));

    private static void OnZoomSliderChanged(DependencyObject dependencyObject,DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        var AdvanceTimeSlider = dependencyObject as NewTimeSlider;

        if (AdvanceTimeSlider != null)
        {
            AdvanceTimeSlider.OnZoomSliderChanged(dependencyPropertyChangedEventArgs);
        }
    }


    private void OnZoomSliderChanged(DependencyPropertyChangedEventArgs e)
    {
        SliderZoom.ValueChanged += SliderZoom_OnValueChanged;
        var _thumb = SliderTimeLine.Template.FindName("Thumb", SliderZoom) as Thumb;
        _thumb.DragCompleted += SliderZoom_OnDragCompleted;
        _thumb.DragStarted += SliderZoom_OnDragStarted;
    }

1 个答案:

答案 0 :(得分:0)

你真的需要绑定吗?为什么不设置SliderZoom属性值?

<local:NewSlider>
    <local:NewSlider.SliderZoom>
        <Slider x:Name="SliderZoom"
                    Width="150"
                    Interval="1"
                    Maximum="10"
                    Minimum="0" />
    </local:NewSlider.SliderZoom>
</local:NewSlider>