首页 > 其他分享 >WPF上位机 - 使用转换器实现TIA Wincc中的位控制可见性或外观功能

WPF上位机 - 使用转换器实现TIA Wincc中的位控制可见性或外观功能

时间:2024-05-03 19:33:04浏览次数:19  
标签:object System value Wincc && using WPF parameter TIA

在TIA Wincc 中有这样的功能,使用True or false 控制控件的可见性或者外观的情况。

在上位机中需要使用转换器这样对True or false 值转换为 需要的笔刷或者Visible属性。

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace SiemensAxisControl.Model.Converter
{
    public class BoolToBrushConverter : IValueConverter
    {
        public object Convert(object value , Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && bool.Parse(value.ToString()))
            {
                return new SolidColorBrush(Color.FromRgb(0,255,0));
            }
            return Brushes.White;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

还有一种情况,不是True or false 的布尔值,而是根据整形值的情况改变控件的可见性或者外观。
如在TIA中以下的情况

  • 变量值等于某一个值改变外观

  • 变量值在某一个区间内改变外观

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace SiemensAxisControl.Model.Converter
{
    internal class IntToBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // 尝试将参数解析为两个整数值,分别代表下限和上限
            if (parameter != null && parameter.ToString().Split('-').Length == 2 &&
                int.TryParse(parameter.ToString().Split('-')[0], out int lowerBound) &&
                int.TryParse(parameter.ToString().Split('-')[1], out int upperBound))
            {
                if(value.GetType() == typeof(short) )
                {
                    // 检查传入的值是否在指定的范围内
                    if (value is short intValue &&  intValue >= lowerBound && intValue <= upperBound)
                    {
                        return new SolidColorBrush(Color.FromRgb(0, 255, 0)); ;
                    }
                }
                else if (value.GetType() == typeof(int))
                 {
                    // 检查传入的值是否在指定的范围内
                    if (value is int intValue && intValue >= lowerBound && intValue <= upperBound)
                    {
                        return new SolidColorBrush(Color.FromRgb(0, 255, 0)); ;
                    }
                }
                
            }
            // 如果不满足条件,则默认为白色
            return Brushes.White;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

标签:object,System,value,Wincc,&&,using,WPF,parameter,TIA
From: https://www.cnblogs.com/Tristan998/p/18171524

相关文章

  • WPF上位机 - S7.NETPlus批量读取数据
    在编写上位机程序中,需要读取PLC数据。使用到了S7.NETPlus这个库。S7.NETPlus提供了很多读取和写入数据的方式。大批量读取数据的方式包括一下几个方法ReadbytesReadClassReadStructReadMultipleVarsPLC侧的数据是一个Array的UDT数据,其中UDT中还包含了很多的UDT。在使用库......
  • WPF上位机 - 轴运动控制
    最近学习WPF,写了一个WPF上位机使用S7.NETPlus库与西门子1500TPLC,控制西门子伺服的通用上位机界面。分享在写上位机过程中踩的一些坑和使用体验。上位机介绍可以看到上位机分为3个区域轴选择,使能区域控制参数设置区域诊断区域选择使能区域选择区域读取TIA中组态的轴工艺......
  • WPF TreeView HierarchicalDataTemplate
    //xaml<Windowx:Class="WpfApp87.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF Text MultiBinding StringFormat
    <TextBlock.Text><MultiBindingStringFormat="R:{0:N0},G:{1:N0},B:{2:N0}"><BindingPath="Value"ElementName="_red"/><BindingPath="Value"ElementName="_green"/>......
  • WPF MultiBinding
    <Windowx:Class="WpfApp84.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF Slider Show integral value TickFrequency="1" IsSnapToTickEnabled="True"
    <Windowx:Class="WpfApp85.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF CollectionViewSource GroupDescriptions GroupStyle ItemsPanelTemplate
    <Windowx:Class="WpfApp83.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF CollectionViewSource GroupDescriptions PropertyGroupDescription GroupStyle
    <Windowx:Class="WpfApp83.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF CollectionViewSource ICollectionViewLiveShaping IsLiveSorting
    <Windowx:Class="WpfApp82.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF DataContext="{Binding SelectedItem,ElementName=_master}"
    <Windowx:Class="WpfApp80.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......