• WPF 值转换


    Value Converter

    首先需要添加引用

    using System.Windows.Data;

    1.实现Bool值取反转换

    internal class bool2InverterConverter : IValueConverter

    {

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

    {

    return !(bool)value;

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

    {

    throw new NotImplementedException();

    }

    }

    UI设计绑定转换类

    <Button Margin=“3” Content=“Start” x:Name=“btnStart”

    IsEnabled=“{Binding LicenseExpired, Converter={StaticResource bool2Inverter}}”/>

    UI 后台代码:

    public partial class MainWindow : Window

    {

    public bool LicenseExpired { get; set; } = true;

    public MainWindow()

    {

    InitializeComponent();

    this.DataContext = this;

    }

    }

    设计一个布尔属性,初始化绑定到自身后,使用窗口类的布尔属性,初始值true,但是经过转换Converter={StaticResource bool2Inverter} 后,取反

    值 True取反,所以Button是Disable

    2. 实现Bool 转换为整型值

    internal class bool2IntConverter : IValueConverter

    {

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

    {

    return (bool)value ? 5:2;

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

    {

    return System.Convert.ToInt32(value) < 5 ? false : true;

    }

    }

    如果是true 返回整数5,否则返回2

    UI设计绑定

    添加Windows 资源:

    <local:bool2IntConverter x:Key=“bool2Int”/>

    绑定Window资源转换类

    <Border Grid.Row=“1” Margin=“3” x:Name=“bd”

    BorderThickness=“{Binding HasThickBorder,Converter={StaticResource bool2Int }}” BorderBrush=“Black”>

    <TextBlock Text=“{Binding ElementName=bd,Path=BorderThickness,UpdateSourceTrigger=PropertyChanged}”

    FontSize=“{Binding BoolProperty ,Converter={StaticResource bool2Int},Mode=OneWayToSource}”/>

    HasThickBorder 窗口类的属性字段

    public bool HasThickBorder { get; set; } = true;

    public bool BoolProperty { get; set; }=true;

    3. 整型转换到字符串

    internal class Int2StringConverter : IValueConverter

    {

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

    {

    if (parameter != null)

    return System.Convert.ToInt32(parameter) >= 60?“Passed”:“Failed”;

    else

    return System.Convert.ToInt32(value)>=60 ?“Passed”:“Failed”;

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

    {

    return System.Convert.ToInt32(value) < 15 ? false : true;

    }

    }

    UI设计

    <local:Int2StringConverter x:Key=“Int2Str”/>

    UI后台代码设计字段:

    public int Grade { get; set; } = 50;

    4. 字符串是否为空转换为可见/不可见属性

    转换类型

    internal class StringEmpty2BoolConverter : IValueConverter

    {

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

    {

    return value.ToString().Equals(string.Empty)?false:true;

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

    {

    return System.Convert.ToInt32(value) < 15 ? false : true;

    }

    }

    UI设计:

    <local:StringEmpty2BoolConverter x:Key=“str2Bool”/>

    输入字符串为空时,Button 不可用,字符串输入不为空时,Enable

  • 相关阅读:
    rabbitMQ:消费者确认模式
    中小学生使用全光谱台灯对眼睛好不好?2023口碑好的护眼台灯推荐
    【HarmonyOS NEXT】鸿蒙customScan (自定义界面扫码)
    Vue3:创建脚手架
    将ECharts图表插入到Word文档中
    Web3中文|元宇宙在商业中的最佳应用
    对全连接层的理解
    Git学习
    kaldi安装流程
    代码随想录算法训练营第56天 | ● 583. 两个字符串的删除操作 ● 72. 编辑距离 ● 动态规划之编辑距离总结篇
  • 原文地址:https://blog.csdn.net/flysh05/article/details/125465520