首页 > 其他分享 >WPF冷却塔组件

WPF冷却塔组件

时间:2025-01-15 16:11:34浏览次数:1  
标签:冷却塔 DependencyProperty ComponentBase value bool typeof 组件 WPF public

1.封装基类ComponentBase.cs 继承自UserControl,定义组件中使用的属性和命令

public class ComponentBase : UserControl
{
    private bool _isSelected;

    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            _isSelected = value;

            if (value)
            {
                var parent = VisualTreeHelper.GetParent(this) as Grid;
                if (parent != null)
                {
                    foreach (var item in parent.Children)
                    {
                        if (item is ComponentBase)
                            (item as ComponentBase).IsSelected = false;
                    }
                }
            }
            VisualStateManager.GoToState(this, value ? "Selected" : "Unselected", false);
        }
    }

    public bool IsRunning
    {
        get { return (bool)GetValue(IsRunningProperty); }
        set { SetValue(IsRunningProperty, value); }
    }
    public static readonly DependencyProperty IsRunningProperty =
        DependencyProperty.Register("IsRunning", typeof(bool), typeof(ComponentBase), new PropertyMetadata(default(bool), new PropertyChangedCallback(OnRunningChanged)));

    private static void OnRunningChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        VisualStateManager.GoToState(d as ComponentBase, (bool)e.NewValue ? "RunState" : "Stop", false);
    }

    public bool IsFaultState
    {
        get { return (bool)GetValue(IsFaultStateProperty); }
        set { SetValue(IsFaultStateProperty, value); }
    }
    public static readonly DependencyProperty IsFaultStateProperty =
        DependencyProperty.Register("IsFaultState", typeof(bool), typeof(ComponentBase), new PropertyMetadata(default(bool), new PropertyChangedCallback(OnFaultStateChanged)));

    private static void OnFaultStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        VisualStateManager.GoToState(d as ComponentBase, (bool)e.NewValue ? "FaultState" : "NormalState", false);
    }

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }
    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(ComponentBase), new PropertyMetadata(default(ICommand)));

    public object CommandParameter
    {
        get { return (object)GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }
    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.Register("CommandParameter", typeof(object), typeof(ComponentBase), new PropertyMetadata(default(object)));

    public ComponentBase()
    {
        this.PreviewMouseLeftButtonDown += ComponentBase_PreviewMouseLeftButtonDown;
    }

    private void ComponentBase_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        this.IsSelected = !this.IsSelected;

        this.Command?.Execute(this.CommandParameter);

        e.Handled = true;
    }
}

 

2.编写设计冷却塔组件CoolingTower.xaml,使用<local:ComponentBase>节点包裹,绑定基础组件类中的上下文

<local:ComponentBase  x:Class="Wpf.Industrial.Controls.CoolingTower"
             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:Wpf.Industrial.Controls"
             mc:Ignorable="d" Cursor="Hand"
             d:DesignHeight="450" d:DesignWidth="800">
    <Border BorderThickness="1" x:Name="frame">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup Name="SelectStateGroup">
                <VisualState Name="Selected">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush)" Storyboard.TargetName="frame">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <SolidColorBrush Color="Orange"/>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState Name="Unselected"/>
            </VisualStateGroup>
            <VisualStateGroup Name="RunStateGroup">
                <VisualState Name="RunState">
                    <Storyboard>
                        <DoubleAnimation Duration="00:0:0.5" From="0" To="-360" RepeatBehavior="Forever"
                                     Storyboard.TargetProperty="(RotateTransform.Angle)"
                                     Storyboard.TargetName="rt"/>

                        <ColorAnimationUsingKeyFrames
                                    Storyboard.TargetName="gsGreen"
                                    Storyboard.TargetProperty="Color">
                            <DiscreteColorKeyFrame Value="Green" KeyTime="0"/>
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState Name="Stop"/>
            </VisualStateGroup>
            <VisualStateGroup x:Name="FaultStateGroup">
                <VisualState Name="FaultState">
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames RepeatBehavior="Forever"
                                    Storyboard.TargetName="gsRed1"
                                    Storyboard.TargetProperty="Color">
                            <DiscreteColorKeyFrame Value="Red" KeyTime="0:0:0.5"/>
                            <DiscreteColorKeyFrame Value="Gray" KeyTime="0:0:1"/>
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState Name="NormalState"/>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Viewbox VerticalAlignment="Center" HorizontalAlignment="Center">
            <Canvas Width="205" Height="245" Margin="5">
                <Polygon Points="0,65 40,10 205,10 180,65" Fill="#EEE"/>

                <Path Data="M0 0A60 15 0 0 0 120 0L120 25A60 15 0 0 1 0 25"  Canvas.Left="45"
                      HorizontalAlignment="Center" Margin="0,15,0,0">
                    <Path.Fill>
                        <LinearGradientBrush EndPoint="1,0" StartPoint="0,0">
                            <GradientStop Color="#FFD6D6D6" Offset="0"/>
                            <GradientStop Color="White" Offset="0.519"/>
                            <GradientStop Color="#FFD6D6D6" Offset="1"/>
                        </LinearGradientBrush>
                    </Path.Fill>
                </Path>

                <Ellipse Width="120" Height="30" Fill="#CCC" VerticalAlignment="Top" Canvas.Left="45"/>

                <Border VerticalAlignment="Top" Canvas.Left="50" Canvas.Top="-40">
                    <Viewbox Width="110" Height="110" RenderTransformOrigin="0.5,0.5">
                        <Viewbox.RenderTransform>
                            <TransformGroup>
                                <RotateTransform Angle="0" x:Name="rt"/>
                                <ScaleTransform ScaleY="0.25"/>
                            </TransformGroup>
                        </Viewbox.RenderTransform>
                        <Path Data="M605.61792 481.6c110.464-39.808 281.6-67.584 376.192 33.536 92.672 98.944 31.168 350.016-167.232 395.904-186.496 43.136-27.456-356.736-246.912-313.6a108.224 108.224 0 0 1-22.4 15.104c38.4 110.592 62.656 276.416-36.224 369.024s-350.08 31.168-395.968-167.232c-41.344-178.816 325.248-39.424 317.44-220.992a107.648 107.648 0 0 1-30.592-44.8c-110.592 36.288-268.032 55.616-357.504-39.68C-50.44608 409.984 11.18592 159.04 210.03392 113.152c179.2-41.28 40.128 323.648 220.608 317.184a107.584 107.584 0 0 1 46.848-23.04c-37.376-110.784-59.648-273.472 37.824-364.8C614.44992-50.496 865.20192 11.136 911.08992 209.984c43.456 188.48-363.328 24.832-312.256 252.928a106.304 106.304 0 0 1 6.784 18.688z" Fill="#EEE">
                        </Path>
                    </Viewbox>
                </Border>

                <Grid Grid.Row="1" Width="180" Height="180" Background="LightGray" Canvas.Top="65">
                    <Border VerticalAlignment="Top" Height="140" BorderThickness="10">
                        <Border.Background>
                            <DrawingBrush TileMode="Tile" ViewportUnits="Absolute" Viewport="1,0,25,1">
                                <DrawingBrush.Drawing>
                                    <GeometryDrawing>
                                        <GeometryDrawing.Pen>
                                            <Pen Brush="#EEE"/>
                                        </GeometryDrawing.Pen>
                                        <GeometryDrawing.Geometry>
                                            <PathGeometry>
                                                <PathFigure>
                                                    <LineSegment Point="10,0"/>
                                                    <LineSegment Point="10,10"/>
                                                </PathFigure>
                                            </PathGeometry>
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>
                                </DrawingBrush.Drawing>
                            </DrawingBrush>
                        </Border.Background>
                    </Border>
                    <Grid VerticalAlignment="Bottom" Height="40" Margin="5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Border Background="#FFAAAAAA" Margin="2"/>
                        <Border Background="#FFAAAAAA" Margin="2" Grid.Column="1"/>
                    </Grid>
                </Grid>

                <Grid Grid.Row="1" Width="180" Height="180" Background="LightGray" Canvas.Top="65" Canvas.Left="180">
                    <Grid.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="0.139"/>
                            <SkewTransform AngleY="-65.5"/>
                        </TransformGroup>
                    </Grid.RenderTransform>
                    <Border VerticalAlignment="Top" Height="140" BorderThickness="10">
                        <Border.Background>
                            <DrawingBrush TileMode="Tile" ViewportUnits="Absolute" Viewport="1,0,25,1">
                                <DrawingBrush.Drawing>
                                    <GeometryDrawing>
                                        <GeometryDrawing.Pen>
                                            <Pen Brush="#EEE"/>
                                        </GeometryDrawing.Pen>
                                        <GeometryDrawing.Geometry>
                                            <PathGeometry>
                                                <PathFigure>
                                                    <LineSegment Point="10,0"/>
                                                    <LineSegment Point="10,10"/>
                                                </PathFigure>
                                            </PathGeometry>
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>
                                </DrawingBrush.Drawing>
                            </DrawingBrush>
                        </Border.Background>
                    </Border>
                    <Grid VerticalAlignment="Bottom" Height="40" Margin="5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Border Background="#FFAAAAAA" Margin="2"/>
                        <Border Background="#FFAAAAAA" Margin="2" Grid.Column="1"/>
                    </Grid>
                </Grid>

                <Border Width="18" Height="18" CornerRadius="10" Canvas.Left="150" Canvas.Top="80">
                    <Border.Background>
                        <RadialGradientBrush>
                            <GradientStop Color="Gray" Offset="0.5" x:Name="gsGreen"/>
                            <GradientStop Color="White"/>
                        </RadialGradientBrush>
                    </Border.Background>
                </Border>
                <Border Width="18" Height="18" CornerRadius="10" Canvas.Left="150" Canvas.Top="105">
                    <Border.Background>
                        <RadialGradientBrush>
                            <GradientStop Color="Gray" Offset="0.6" x:Name="gsRed1"/>
                            <GradientStop Color="White"/>
                        </RadialGradientBrush>
                    </Border.Background>
                </Border>
            </Canvas>
        </Viewbox>
    </Border>
</local:ComponentBase>

后台代码继承ComponentBase基类

    /// <summary>
    /// CoolingTower.xaml 的交互逻辑
    /// </summary>
    public partial class CoolingTower : ComponentBase
    {
        public CoolingTower()
        {
            InitializeComponent();
        }
    }

 

3.使用该组件

         <!--冷却塔-->
         <control:CoolingTower Width="90" Height="110"
                               Command="{Binding ComponentCommand}"

                               HorizontalAlignment="Left" 
                               VerticalAlignment="Bottom" 
                               Margin="125,0,0,95"/>

 

标签:冷却塔,DependencyProperty,ComponentBase,value,bool,typeof,组件,WPF,public
From: https://www.cnblogs.com/jiangyuhu/p/18673261

相关文章

  • WPF 使用GalaSoft.MvvmLight实现MVVM
    MVVM(Model-View-ViewModel)是一种设计模式,用于分离用户界面(View)与业务逻辑(ViewModel)和数据模型(Model)。这种模式特别适用于WPF(WindowsPresentationFoundation)应用程序,因为它可以充分利用WPF的数据绑定、命令和依赖属性等特性,提高应用程序的可维护性和可测试性。MVVM的三个......
  • vue组件大全
    作用此随笔记录我人生中所有自己写出来的组件样式图片加文字组件<template><divclass="set_img"><el-container><el-asidewidth="300px"><pclass="set_img_p">弹框一:</p>......
  • 封装按钮信息与按钮数量动态显示与提示信息并进行触发按钮组件
    标题:封装弹框并点击动态按钮组件进行触发功能:封装按钮信息与按钮数量动态显示组件页面:按钮信息与按钮数量动态显示,提示信息不固定封装组件页面: <template><el-dialogv-model="dialogVisible"class="stepCustom-dialogcustom-dialog-center"destroy-on-closeheight=......
  • 图表封装组件
    图表封装: 图表封装使用:<template><!--软件模块复用率图表--><chart-and-tableref="chartAndTableRef":chart-desc="chartDesc":chart-loading="chartLoading":columns="columns":list="list":......
  • 【WPF】使用RenderTargetBitmap截图的时候位置出现偏移的一些解决办法
    简介在WPF中,如果你需要把控件的渲染画面保存到图片,那么唯一的选择就是RenderTargetBitmap。不过,RenderTargetBitmap是个比较难伺候的主,有时候你以为能工作,但实际上不能;你以为能够正常截图,但实际上截出来的图片是歪的。所以,我总结一下自己项目中遇到的坑和解决办法吧!保存的图片......
  • DevExpress WPF 中文教程:Grid - 如何创建列并将其绑定到数据属性?
    DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。无论是Office办公软件的衍伸产品,还是以数据为中心......
  • vuex 构建多组件共享的数据环境
    Vuex是Vue.js的官方状态管理库,用于在Vue.js应用程序中构建多个组件共享的数据环境。Vuex可以帮助我们解决组件之间共享数据和状态管理的问题。它将应用程序的状态存储在一个单一的地方,称为"store",并且允许组件直接从store中获取和修改状态,而不需要通过事件或属性传递......
  • 《鸿蒙开发-鸿蒙教程-答案之书》组件margin左和右等于没偏?
    《鸿蒙开发-鸿蒙教程-答案之书》组件margin左和右等于没偏?鸿蒙的margin和Android的margin还是有点区别,Android的margin没有任何毛病,但是鸿蒙margin会导致组件偏移回来的。因为鸿蒙的组件可以超越父组件,往外面宽展。那怎么办呢?先看个有问题的代码:@Entry@Componentstruc......
  • Vue.js组件开发-使用地图绘制轨迹
    在Vue.js中开发一个组件来展示地图并绘制轨迹,可以使用诸如Leaflet.js、MapboxGLJS或百度地图等地图库。这些库提供了丰富的API来创建和定制地图,以及绘制路径、标记和其他地图元素。示例:1.安装Leaflet.js首先,需要安装Leaflet.js库。可以通过npm或yarn来安装:npminstalll......
  • 深度剖析RabbitMQ:从基础组件到管理页面详解
    文章目录一、简介二、Overview2.1Overview->Totals2.2Overview->Nodesbroker的属性2.3Overview->Churnstatistics2.4Overview->Portsandcontexts2.5Overview->Exportdefinitions2.6Overview->Importdefinitions三、Connections连接的属性四、Channels通道的......