当前位置:网站首页>WPF value conversion
WPF value conversion
2022-06-26 03:59:00 【flysh05】
Value Converter
First you need to add references
using System.Windows.Data;
1. Realization Bool Value inversion conversion
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 Design binding transformation class
<Button Margin=“3” Content=“Start” x:Name=“btnStart”
IsEnabled=“{Binding LicenseExpired, Converter={StaticResource bool2Inverter}}”/>
UI Background code :
public partial class MainWindow : Window
{
public bool LicenseExpired { get; set; } = true;
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
}
Design a Boolean property , After initializing binding to itself , Use the Boolean property of the window class , Initial value true, But after conversion Converter={StaticResource bool2Inverter} after , Take the opposite
value True Take the opposite , therefore Button yes Disable
2. Realization Bool Convert to integer value
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;
}
}
If it is true Return integer 5, Otherwise return to 2
UI Design binding
add to Windows resources :
<local:bool2IntConverter x:Key=“bool2Int”/>
binding Window Resource conversion class
<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 Property field of window class
public bool HasThickBorder { get; set; } = true;
public bool BoolProperty { get; set; }=true;
3. Integer to string
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 Design
<local:Int2StringConverter x:Key=“Int2Str”/>
UI Background code design field :
public int Grade { get; set; } = 50;
4. Whether the string is null is converted to visible / Invisible properties
Conversion type
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 Design :
<local:StringEmpty2BoolConverter x:Key=“str2Bool”/>
When the input string is null ,Button Unavailable , When the string input is not empty ,Enable
边栏推荐
- 【QT】对话框dialog
- Dix critères de base importants pour les essais de débogage de logiciels
- go语言泛型在IDE中语法报错
- Kotlin uses viewpager2+fragment+bottomnavigationview to implement the style of the switching module of the bottom menu bar.
- 力扣 515. 在每个树行中找最大值
- bubble sort
- Spark - 一文搞懂 parquet
- Webrtc series - 6-connections tailoring for network transmission
- 2020 summary: industrial software development under Internet thinking
- Open Camera异常分析(一)
猜你喜欢

Open Camera异常分析(一)
![[collection of good books] from technology to products](/img/b1/a119d61ff20d19d1e42e5c243de42c.png)
[collection of good books] from technology to products

(15)Blender源码分析之闪屏窗口显示菜单功能

When the tiflash function is pushed down, it must be known that it will become a tiflash contributor in ten minutes

Detr3d multi 2D picture 3D detection framework

力扣 515. 在每个树行中找最大值
![[Flink] Flink source code analysis - creation of jobgraph in batch mode](/img/8e/1190eec23169a4d2a06e1b03154d4f.jpg)
[Flink] Flink source code analysis - creation of jobgraph in batch mode

线程同步之互斥量(互斥锁)

高性能算力中心 — RoCE — Overview

MapReduce execution principle record
随机推荐
Mybatis的引入问题invalid
ABP framework Practice Series (II) - Introduction to domain layer
I/o virtualization technology - vfio
Link monitoring pinpoint
商城风格也可以很多变,DIY 了解一下
阿里云函数计算服务一键搭建Z-Blog个人博客
[LOJ 6718] nine suns' weakened version (cyclic convolution, arbitrary modulus NTT)
Introduction Guide to the flutterplugin plug-in in the actual combat of flutter
QPS的概念和实现
刷题记录Day01
High performance computing center roce overview
【QT】对话框dialog
Is the waiting insurance record a waiting insurance evaluation? What is the relationship between the two?
Conditional variables for thread synchronization
Introduction of mybatis invalid
R language and machine learning
Webrtc series - 6-connections tailoring for network transmission
Uni app, the text implementation expands and retracts the full text
如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
[从零开始学习FPGA编程-45]:视野篇 - 集成电路助力数字化时代高质量发展-2-市场预测