WPF - data binding trigger before content changed
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"));
}
}
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"开发者_Python百科 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 ------------
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);
}
}
Make "ActiveView" a dependency property and use value coercion mechanism and do you stuff in the Coerce value callback.
Hope it helps!!
精彩评论