首页 > 解决方案 > 如何在行为中将绑定添加到 xaml 绑定?

问题描述

我有一个 xaml 接口的行为:

<i:Interaction.Behaviors>
      <b:ChartCanvasItemBehavior
               Source="{Binding MovieUri}"                                    
              Timeline="{Binding Path=Timeline, ElementName=ChartCanvasRightPanel, Mode=OneWayToSource}" 
                                                             
       />
</i:Interaction.Behaviors>

该行为创建了一个装饰器,它执行以下操作:

public double Timeline
        {
            get { return (double)GetValue(TimelineProperty); }
            set { SetValue(TimelineProperty, value); }
        }

    // Using a DependencyProperty as the backing store for Timeline.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TimelineProperty =
        DependencyProperty.Register("Timeline", typeof(double), typeof(MovieControlForChartCanvas), new UIPropertyMetadata(0.0));

为了将两者联系起来,我有:

var bindingMovieTime = new Binding
            {
                Source = mc,                                         
                Path = new PropertyPath(MovieControlForChartCanvas.TimelineProperty) 
            };
            BindingOperations.SetBinding(this, iTimelineProperty, bindingMovieTime);


#region [Dependency Properties used to bind to the MovieControlForChartCanvas]
        // These dependecy properties have to be DIFFERENT then the dependency properties used for the ChartCanvasItemBehavior xaml interface. 
        // Using the same dependecy property for both bindings to the MovieControl and the Xaml interface results in losing one of the first bindings (i.e., the xaml binding).
        public double iTimeline
        {
            get { return (double)GetValue(iTimelineProperty); }
            set { SetValue(iTimelineProperty, value); }
        }

        // Using a DependencyProperty as the backing store for iTimeline.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty iTimelineProperty =
            DependencyProperty.Register("iTimeline", typeof(double), typeof(ChartCanvasItemBehavior), new PropertyMetadata(0.0,
                (s, e) =>
                {
                    ChartCanvasItemBehavior d = s as ChartCanvasItemBehavior;
                    d.Timeline = (double)e.NewValue;
                }
                ));



        #endregion


        public double Timeline
        {
            get { return (double)GetValue(TimelineProperty); }
            set { SetValue(TimelineProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Timeline.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TimelineProperty =
            DependencyProperty.Register("Timeline", typeof(double), typeof(ChartCanvasItemBehavior  ), new PropertyMetadata(0.0));

这似乎需要行为和装饰者之间的第二个属性。如果我对行为接口和行为到装饰器接口都使用一个属性,则对该行为的第一个分配(即来自 xaml)将被与装饰器的绑定覆盖。

这可以在多重绑定的行为中仅使用一个依赖属性来完成吗?如果是这样,怎么做?

换句话说,是否可以在行为中使用一个依赖属性作为 xaml 和装饰器的接口?(多重绑定?)

感谢您的任何建议。

标签: wpfbehaviormultibinding

解决方案


推荐阅读