如何在WPF中的UserControl中将属性绑定到矩形的样式

时间:2019-05-31 12:04:59

标签: c# wpf xaml data-binding styles

我有一个矩形,该矩形的样式在中定义,但是样式内的数据绑定不起作用?如何解决该问题?

如果我覆盖矩形,它将起作用(但动画不起作用)。但是,如果我将其移至样式,它将不再起作用。

<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 :(得分:0)

似乎没有解决方案……我必须通过定义不同的样式并相应地应用它们来解决。我很确定这不是推荐的方法,但是至少目前它对我有用。我希望任何知道如何解决此问题的人都可以在这里慷慨地发表您的答案。

我当前的解决方案/解决方法是(单击以查看图像): xmal code behind