187 lines
6.3 KiB
C#
187 lines
6.3 KiB
C#
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;
|
||
using System.Collections;
|
||
using MegaRobo.ControlDevices;
|
||
using Common.Models;
|
||
using System.Windows.Media;
|
||
|
||
namespace MegaRobo.C00225155App.Converters
|
||
{
|
||
public class BooleanToVisibilityConverter : IValueConverter
|
||
{
|
||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
if (value == null)
|
||
{
|
||
return Visibility.Collapsed;
|
||
}
|
||
|
||
if (value is bool boolValue)
|
||
{
|
||
return boolValue ? Visibility.Visible : Visibility.Collapsed;
|
||
}
|
||
return Visibility.Collapsed;
|
||
}
|
||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
if (value is Visibility visibilityValue)
|
||
{
|
||
return visibilityValue == Visibility.Visible;
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public class VerseBooleanConverter : IValueConverter
|
||
{
|
||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
if (value is bool boolValue)
|
||
{
|
||
return !boolValue;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
if (value is bool boolValue)
|
||
{
|
||
return !boolValue;
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public class BoolToTagConverter : IValueConverter
|
||
{
|
||
/// <summary>
|
||
/// 将bool值转换为Tag字符串
|
||
/// </summary>
|
||
/// <param name="value">bool值</param>
|
||
/// <param name="targetType">目标类型</param>
|
||
/// <param name="parameter">转换参数</param>
|
||
/// <param name="culture">文化信息</param>
|
||
/// <returns>转换后的Tag字符串</returns>
|
||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
// 如果值是bool类型
|
||
if (value is bool boolValue)
|
||
{
|
||
// true返回"Manual",false返回"Auto"(可根据需要调整)
|
||
return boolValue ? "Manual" : "Auto";
|
||
}
|
||
|
||
// 默认返回"Auto"
|
||
return "Auto";
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将Tag字符串转换为bool值(双向绑定使用)
|
||
/// </summary>
|
||
/// <param name="value">Tag字符串</param>
|
||
/// <param name="targetType">目标类型</param>
|
||
/// <param name="parameter">转换参数</param>
|
||
/// <param name="culture">文化信息</param>
|
||
/// <returns>转换后的bool值</returns>
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
// 如果值是字符串
|
||
if (value is string tagValue)
|
||
{
|
||
// "Manual"返回true,"Auto"返回false(与Convert保持一致)
|
||
return string.Equals(tagValue, "Manual", StringComparison.OrdinalIgnoreCase);
|
||
}
|
||
|
||
// 默认返回false
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据集合数量转换为可见性的转换器
|
||
/// 集合数量 > 0 时显示(Visibility.Visible)
|
||
/// 集合数量 = 0 时隐藏(Visibility.Collapsed)
|
||
/// </summary>
|
||
public class CollectionCountToVisibilityConverter : IValueConverter
|
||
{
|
||
/// <summary>
|
||
/// 转换方法:集合数量 -> 可见性
|
||
/// </summary>
|
||
/// <param name="value">绑定的集合</param>
|
||
/// <param name="targetType">目标类型</param>
|
||
/// <param name="parameter">参数</param>
|
||
/// <param name="culture">文化信息</param>
|
||
/// <returns>可见性枚举值</returns>
|
||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
// 检查是否为集合类型
|
||
if (value is IEnumerable collection)
|
||
{
|
||
// 计算集合中的元素数量
|
||
int count = 0;
|
||
foreach (var item in collection)
|
||
{
|
||
count++;
|
||
}
|
||
|
||
// 根据数量返回对应的可见性
|
||
return count > 0 ? Visibility.Visible : Visibility.Collapsed;
|
||
}
|
||
|
||
// 如果不是集合类型,默认隐藏
|
||
return Visibility.Collapsed;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 反向转换方法:可见性 -> 集合数量(通常不需要实现)
|
||
/// </summary>
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
// 因为可见性无法反向转换为集合数量,所以抛出异常
|
||
throw new NotImplementedException("不支持反向转换");
|
||
}
|
||
}
|
||
|
||
public class WorkStatusToBackgroundConvter : IValueConverter
|
||
{
|
||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
// 增加类型检查
|
||
if (value == null||!(value is ControlWorkStatus workStatus))
|
||
{
|
||
return Brushes.LightGray; // 默认灰色
|
||
}
|
||
|
||
switch (workStatus.ExecuteStatus)
|
||
{
|
||
case ExecuteStatus.None:
|
||
return Brushes.LightGray;
|
||
case ExecuteStatus.Ongoing:
|
||
return Brushes.LimeGreen; // 绿色
|
||
case ExecuteStatus.Error:
|
||
return Brushes.Red; // 错误状态设为红色
|
||
// 可以添加其他状态的颜色
|
||
default:
|
||
return Brushes.LightGray;
|
||
}
|
||
//return Brushes.LightGray; // 默认灰色
|
||
}
|
||
|
||
/// <summary>
|
||
/// 反向转换方法:可见性 -> 集合数量(通常不需要实现)
|
||
/// </summary>
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
throw new NotImplementedException("不支持反向转换");
|
||
}
|
||
}
|
||
}
|