Как связать свойство со стилем прямоугольника в UserControl в WPF - PullRequest
0 голосов
/ 31 мая 2019

У меня есть прямоугольник со стилем, определенным в , но привязка данных в этом стиле не работает? Как решить проблему?

Если я переопределить прямоугольник, он работает (но анимация не работает). но если я переместил его в стиль, он больше не работает.

<UserControl x:Class="MyProject.WPF.Controls.UI_MovingLine"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:AutinPower.WPF.Controls"
             mc:Ignorable="d" 
             Height="8" Width="auto">

    <Rectangle x:Name="rectangleLine" Width="auto" Height="auto" Margin="0">
        <Rectangle.Style>
            <Style TargetType="{x:Type Rectangle}">
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <RotateTransform Angle="0" />
                    </Setter.Value>
                </Setter>
                <Setter Property="Fill">
                    <Setter.Value>
                        <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>
                    </Setter.Value>
                </Setter>
                <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>
        </Rectangle.Style>
    </Rectangle>
</UserControl>

код позади

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MyProject.Enum;

namespace MyProject.WPF.Controls
{
    /// <summary>
    /// Interaction logic for UI_MovingLine.xaml
    /// </summary>
    public partial class UI_MovingLine : UserControl, INotifyPropertyChanged
    {
        #region properties
        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 PowerSystem.TransmitLineStatus TransmitLineStatus { get => _transmitLineStatus; set => _transmitLineStatus = value; }
        public string AnimationStart {
            get => _animationStart;
            set {
                _animationStart = value;
                NotifyPropertyChanged("AnimationStart");
            } }
        public Action<object[]> NotifyUI { get; set; }
        #endregion

        #region fields
        private PowerSystem.TransmitLineStatus _transmitLineStatus;
        private Storyboard _storyboard;
        private string _animationStart;
        #endregion

        public UI_MovingLine()
        {
            InitializeComponent();
            rectangleLine.DataContext = this;
            //gridMoving.DataContext = this;
            //polygonMoving.DataContext = this;
            AnimationStart = "Start";
        }

        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)*/));

        public static readonly DependencyProperty AnimationStartProperty
            = DependencyProperty.Register("AnimationStart", typeof(string), typeof(UI_MovingLine), new PropertyMetadata("Stop"/*, new PropertyChangedCallback(AnimationStartChanged)*/));

        public void LineMovingToNomal()
        {
            try
            {
                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
            {
            }
        }
}

привязка данных в стиле должна работать правильно

1 Ответ

0 голосов
/ 02 июня 2019

Кажется, для этого нет решения ... Мне нужно обойтись, определив разные стили и применив их соответствующим образом. Я совершенно уверен, что это не рекомендуемый способ сделать это, но по крайней мере это работает для меня в данный момент. Я надеюсь, что любой, кто знает, как решить эту проблему, может щедро опубликовать здесь свои ответы.

Мое текущее решение / обходной путь (нажмите, чтобы увидеть изображения): xmal код позади

...