繁体   English   中英

如何在c#wpf的usercontrol中使用datatrigger for rectangle?

[英]How to use datatrigger for rectangle in a usercontrol in c# wpf?

我在wpf c#中创建了一个带有复杂矩形的UserControl,我想用一个属性的数据触发器来触发动画。 如果我在矩形中使用eventtrigger,它可以按预期工作,但是如果我切换到datatrigger,它就无法为矩形设置动画...对于动画,请点击发布方向更改WPF移动箭头的 @Val

  1. 我已经尝试在<Rectangle.Resource>中包含storyboard,并在代码中使用_storyboard.begin(),但是没有动画它。
  2. 我已经尝试将storyboard包含在一个包含一个绑定到名为AnimationStart的属性的数据触发器中。 我也尝试将<Style>放在<UserControl.Resource>和<Rectangle.Resource>中,但都没有用。
  3. 我还为AnimationStart实现了INotifyPropertyChanged或DependencyProperty,但它们仍然没有工作。
  4. DependencyProperty:IndicationPoints,LineBackgroundColor,LineBackColor正常工作。

现在xaml如下

 <UserControl.Resources>
        <Style x:Key="animationStyle" TargetType="{x:Type Rectangle}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding AnimationStart}" Value="Start">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard x:Name="StartAnimation">
                            <Storyboard>
                                <DoubleAnimation 
                                    From="0" 
                                    To="10" 
                                    RepeatBehavior="Forever"
                                    Storyboard.TargetProperty="(Rectangle.Fill).(VisualBrush.Transform).(TranslateTransform.X)" 
                                    Duration="0:0:0.1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
                <DataTrigger Binding="{Binding AnimationStart}" Value="Stop">
                    <DataTrigger.EnterActions>
                        <StopStoryboard x:Name="StopAnimation" BeginStoryboardName="StartAnimation"/>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <Rectangle x:Name="rectangleLine" Style="{StaticResource animationStyle}" Width="auto" Height="auto" Margin="0">
        <Rectangle.RenderTransform>
            <RotateTransform Angle="0" />
        </Rectangle.RenderTransform>
        <Rectangle.Fill>
            <VisualBrush TileMode="Tile" Viewport="0,0,10,8" ViewportUnits="Absolute" Viewbox="0,0,8,8" ViewboxUnits="Absolute">
                <VisualBrush.Transform>
                    <TranslateTransform X="0" Y="0" />
                </VisualBrush.Transform>
                <VisualBrush.Visual>
                    <Grid x:Name="gridMoving">
                        <Polygon x:Name="polygonMovingBack" Fill="{Binding LineBackColor}" Points="0,0 8,0 8,8 0,8" />
                        <Polygon x:Name="polygonMoving" Fill="{Binding LineBackgroundColor}" Points="{Binding IndicationShape}" />
                    </Grid>
                </VisualBrush.Visual>
            </VisualBrush>
        </Rectangle.Fill>
        <Rectangle.Resources>
            <Storyboard x:Key="startAnimation">
                <DoubleAnimation 
                    From="0" 
                    To="10" 
                    RepeatBehavior="Forever"
                    Storyboard.TargetProperty="(Rectangle.Fill).(VisualBrush.Transform).(TranslateTransform.X)" 
                    Duration="0:0:0.1" />
            </Storyboard>
        </Rectangle.Resources>

        <!--<Rectangle.Triggers>
            <EventTrigger RoutedEvent="Control.Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation 
                                From="0" 
                                To="10" 
                                RepeatBehavior="Forever"
                                Storyboard.TargetProperty="(Rectangle.Fill).(VisualBrush.Transform).(TranslateTransform.X)" 
                                Duration="0:0:0.1" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Rectangle.Triggers>-->
    </Rectangle>

和背后的代码


        public PointCollection IndicationPoints
        {
            get
            {
                return (PointCollection)GetValue(IndicationPointsProperty);
            }
            set
            {
                SetValue(IndicationPointsProperty, value);
            }
        }
        public SolidColorBrush LineBackgroundColor
        {
            get
            {
                return (SolidColorBrush)GetValue(LineBackgroundColorProperty);
            }
            set
            {
                SetValue(LineBackgroundColorProperty, value);
            }
        }
        public SolidColorBrush LineBackColor
        {
            get
            {
                return (SolidColorBrush)GetValue(LineBackColorProperty);
            }
            set
            {
                SetValue(LineBackColorProperty, value);
            }
        }
        public string AnimationStart {
            get => _animationStart;
            set {
                _animationStart = value;
                NotifyPropertyChanged("AnimationStart");
            } }

        private string _animationStart;

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public static readonly DependencyProperty IndicationPointsProperty
            = DependencyProperty.Register("IndicationPoints", typeof(PointCollection), typeof(UI_MovingLine), new PropertyMetadata(new PointCollection(new List<Point> { new Point(0,0), new Point(4, 0), new Point(8, 4), new Point(4, 8), new Point(0, 8), new Point(4, 4) }), new PropertyChangedCallback(OnIndicationPointsChanged)));

        public static readonly DependencyProperty LineBackgroundColorProperty
            = DependencyProperty.Register("LineBackgroundColor", typeof(SolidColorBrush), typeof(UI_MovingLine), new PropertyMetadata(new SolidColorBrush(Colors.Gray), new PropertyChangedCallback(OnLineBackgroundColorChanged)));

        public static readonly DependencyProperty LineBackColorProperty
            = DependencyProperty.Register("LineBackColor", typeof(SolidColorBrush), typeof(UI_MovingLine), new PropertyMetadata(new SolidColorBrush(Colors.Gray), new PropertyChangedCallback(OnLineBackColorChanged)));

        private static void OnIndicationPointsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as UI_MovingLine;
            control.OnIndicationPointsChanged(e);
        }

        private void OnIndicationPointsChanged(DependencyPropertyChangedEventArgs e)
        {
            polygonMoving.Points = (PointCollection)e.NewValue;
        }

        private static void OnLineBackgroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as UI_MovingLine;
            control.OnLineBackgroundColorChanged(e);
        }

        private void OnLineBackgroundColorChanged(DependencyPropertyChangedEventArgs e)
        {
            polygonMoving.Fill = (SolidColorBrush)e.NewValue;
        }

        private static void OnLineBackColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as UI_MovingLine;
            control.OnLineBackColorChanged(e);
        }

        private void OnLineBackColorChanged(DependencyPropertyChangedEventArgs e)
        {
            polygonMovingBack.Fill = (SolidColorBrush)e.NewValue;
        }

        public UI_MovingLine()
        {
            InitializeComponent();
            gridMoving.DataContext = this;
            polygonMoving.DataContext = this;
            LineMovingToNomal();
        }

        public void LineMovingToNomal()
        {
            try
            {
                _transmitLineStatus = PowerSystem.TransmitLineStatus.TLS_NORMAL;
                IndicationPoints = new PointCollection(new List<Point> { new Point(0, 0), new Point(4, 0), new Point(8, 4), new Point(4, 8), new Point(0, 8), new Point(4, 4) });
                LineBackgroundColor = new SolidColorBrush(Colors.DodgerBlue);
                LineBackColor = new SolidColorBrush(Colors.White);

                AnimationStart = "Start";
            }
            catch
            {
            }
        }

如果没有MCVE,有点难以帮助你跟踪它,但尝试使用样式中的setter设置RenderTransform和Fill属性,而不是试图在Rectangle控件本身中覆盖它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM