【问题标题】:C# WPF: Toggle Margin value using Storyboard and EventTriggerC# WPF:使用 Storyboard 和 EventTrigger 切换边距值
【发布时间】:2026-01-22 10:35:01
【问题描述】:

所以我有这个 WPF 代码:

<EventTrigger RoutedEvent="PreviewMouseRightButtonDown">
    <EventTrigger.Actions>
        <BeginStoryboard Name="Storyboard">
            <Storyboard>
                <ThicknessAnimation Storyboard.TargetProperty="Margin">
                    ...
                </ThicknessAnimation>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger.Actions>
</EventTrigger>

有没有办法将下边距从一个值切换到另一个值,例如:

Margin = Margin.Bottom is 0 ? new Thickness(0, 0, 0, -50) : new Thickness(0);

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    您可以为厚度设置动画,这是一个示例 https://docs.microsoft.com/en-us/dotnet/desktop/wpf/controls/how-to-animate-a-borderthickness-value?view=netframeworkdesktop-4.8

    代码复制到这里

    namespace SDKSamples
    {
        public class ThicknessAnimationExample : Page
        {
            public ThicknessAnimationExample()
            {
                // Create a NameScope for this page so that
                // Storyboards can be used.
                NameScope.SetNameScope(this, new NameScope());
    
                // Create a Border which will be the target of the animation.
                Border myBorder = new Border();
                myBorder.Background = Brushes.Gray;
                myBorder.BorderBrush = Brushes.Black;
                myBorder.BorderThickness = new Thickness(1);
                myBorder.Margin = new Thickness(0, 60, 0, 20);
                myBorder.Padding = new Thickness(20);
    
                // Assign the border a name so that
                // it can be targeted by a Storyboard.
                this.RegisterName( "myAnimatedBorder", myBorder);
    
                ThicknessAnimation myThicknessAnimation = new ThicknessAnimation();
                myThicknessAnimation.Duration = TimeSpan.FromSeconds(1.5);
                myThicknessAnimation.FillBehavior = FillBehavior.HoldEnd;
    
                // Set the From and To properties of the animation.
                // BorderThickness animates from left=1, right=1, top=1, and bottom=1
                // to left=28, right=28, top=14, and bottom=14 over one and a half seconds.
                myThicknessAnimation.From = new Thickness(1, 1, 1, 1);
                myThicknessAnimation.To = new Thickness(28, 14, 28, 14);
    
                // Set the animation to target the Size property
                // of the object named "myArcSegment."
                Storyboard.SetTargetName(myThicknessAnimation, "myAnimatedBorder");
                Storyboard.SetTargetProperty(
                    myThicknessAnimation, new PropertyPath(Border.BorderThicknessProperty));
    
                // Create a storyboard to apply the animation.
                Storyboard ellipseStoryboard = new Storyboard();
                ellipseStoryboard.Children.Add(myThicknessAnimation);
    
                // Start the storyboard when the Path loads.
                myBorder.Loaded += delegate(object sender, RoutedEventArgs e)
                {
                    ellipseStoryboard.Begin(this);
                };
    
                StackPanel myStackPanel = new StackPanel();
                myStackPanel.HorizontalAlignment = HorizontalAlignment.Center;
                myStackPanel.Children.Add(myBorder);
    
                Content = myStackPanel;
            }
        }
    }
    

    【讨论】:

    • 您好,感谢您在 XAML 中的回复,我可以在后面的代码中轻松完成
    • @ynsbl.eng IMO 后面的代码很清楚,而不是教堂后面的无代码,但也许这个问题对你有帮助 *.com/questions/7292540/…