WPF - 内容更改前的数据绑定触发

问题描述:

如何创建触发器,在绑定更改值之前触发?如何做这个datatemplate?

How do i create trigger, which fires BEFORE binding changes value? How to do this for datatemplate?

<ContentControl Content="{Binding Path=ActiveView}" Margin="0,95,0,0">
    <ContentControl.Triggers>
        <--some triger to fire, when ActiveView is changing or has changed ?!?!? -->
    </ContentControl.Triggers>





public Object ActiveView
{
    get { return m_ActiveView; }
    set {
        if (PropertyChanging != null)
            PropertyChanging(this, new PropertyChangingEventArgs("ActiveView"));
        m_ActiveView = value;
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("ActiveView"));
    } 
}

如何使用DataTemplate?

How to do this for DataTemplate?

<DataTemplate DataType="{x:Type us:LOLClass1}">
            <ContentControl>
                <ContentControl.RenderTransform>
                    <ScaleTransform x:Name="shrinker" CenterX="0.0" CenterY="0.0" ScaleX="1.0" ScaleY="1.0"/>
                </ContentControl.RenderTransform>
                <us:UserControl1/>
            </ContentControl>
            <DataTemplate.Triggers>
                <-- SOME TRIGER BEFORE CONTENT CHANGES-->
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="shrinker" Storyboard.TargetProperty="ScaleX" From="1.0" To="0.8" Duration="0:0:0.3"/>
                            <DoubleAnimation Storyboard.TargetName="shrinker" Storyboard.TargetProperty="ScaleY" From="1.0" To="0.8" Duration="0:0:0.3"/>
                        </Storyboard>
                    </BeginStoryboard>
                </-- SOME TRIGER BEFORE CONTENT CHANGES-->
            </DataTemplate.Triggers>
        </DataTemplate>

绑定更改之前如何获取通知? (我想捕获更改的视觉组件到位图并创建滑动视图动画)

How to get notification BEFORE binding is changed? (i want to capture changing Visual component to bitmap and create sliding view animation)

---------解决方案--------- ---

--------- SOLUTION ------------

我创建了从ContentControl派生的自定义控件,并覆盖了ContentProperty ValueChanged回调

I created custom control derived from ContentControl and overrided ContentProperty ValueChanged callback

public class SmartContentControl : ContentControl
{
    public static readonly RoutedEvent ContentChangingEvent = EventManager.RegisterRoutedEvent("ContentChanging", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SmartContentControl));
    public event RoutedEventHandler ContentChanging
    {
        add { AddHandler(ContentChangingEvent, value); }
        remove { RemoveHandler(ContentChangingEvent, value); }
    }

    public SmartContentControl()
    {
        ContentProperty.OverrideMetadata(typeof(SmartContentControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(ContentPropertyChangedCallback)));
    }

    private static void ContentPropertyChangedCallback(DependencyObject _object, DependencyPropertyChangedEventArgs _eventArgs)
    {
        SmartContentControl control = (SmartContentControl)_object;
        RoutedEventArgs newEventArgs = new RoutedEventArgs(SmartContentControl.ContentChangingEvent);
        control.RaiseEvent(newEventArgs);
    }
}


ActiveView是一个依赖属性,并使用价值强制机制,并且你东西在Coerce值回调。

Make "ActiveView" a dependency property and use value coercion mechanism and do you stuff in the Coerce value callback.

希望它有帮助!!