当前位置:网站首页>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
边栏推荐
- 【Flink】Flink Sort-Shuffle写流程简析
- Quanergy欢迎Lori Sundberg出任首席人力资源官
- 高性能算力中心 — RoCE — Overview
- Lua语法讲解
- MapReduce执行原理记录
- [Flink] Flink source code analysis - creation of jobgraph in batch mode
- 商城风格也可以很多变,DIY 了解一下
- 2021 year end summary
- Uni app swiper rotation chart (full screen / card)
- [从零开始学习FPGA编程-45]:视野篇 - 集成电路助力数字化时代高质量发展-2-市场预测
猜你喜欢
商城风格也可以很多变,DIY 了解一下
Small record of neural network learning 71 - tensorflow2 deep learning with Google Lab
[collection of good books] from technology to products
ABP framework Practice Series (III) - domain layer in depth
【Flink】Flink源码分析——批处理模式JobGraph的创建
Quanergy welcomes Lori sundberg as chief human resources officer
线程同步之条件变量
After a test of 25K bytes, I really saw the basic ceiling
YOLOv5改进:更换骨干网(Backbone)
(15) Blender source code analysis flash window display menu function
随机推荐
Getting started with flask
Detailed explanation of globalkey of flutter
【掘金运营套路揭露】真心被掘金的套路....
R language and machine learning
C # knowledge structure
软件调试测试的十大重要基本准则
Restful API interface design standards and specifications
1.基础关
go time包:秒、毫秒、纳秒时间戳输出
I/O 虚拟化技术 — UIO Framework
线程同步之条件变量
169. 多数元素
816. fuzzy coordinates
【QT】资源文件导入
Ten important basic principles of software debugging and testing
(15)Blender源码分析之闪屏窗口显示菜单功能
WPF 值转换
Yolov5 improvements: replace the backbone
Webrtc series - 7-ice supplement of network transmission preference and priority
YOLOv5改进:更换骨干网(Backbone)