当前位置:网站首页>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源码分析——批处理模式JobGraph的创建

1.基础关
![[LOJ 6718] nine suns' weakened version (cyclic convolution, arbitrary modulus NTT)](/img/fd/0c299b7cc728f2d6274eea30937726.png)
[LOJ 6718] nine suns' weakened version (cyclic convolution, arbitrary modulus NTT)

What should I do if the 51 SCM board cannot find the device in keil

Evaluation - analytic hierarchy process

軟件調試測試的十大重要基本准則

高性能算力中心 — RoCE — Overview

What's wrong with connecting MySQL database with eclipse and then the words in the figure appear

Verrouillage de lecture et d'écriture pour la synchronisation des fils

外包干了四年,人直接废了。。。
随机推荐
Dix critères de base importants pour les essais de débogage de logiciels
English version of ternary loss
Go time package: second, millisecond, nanosecond timestamp output
Oracle technology sharing Oracle 19.14 upgrade 19.15
[从零开始学习FPGA编程-45]:视野篇 - 集成电路助力数字化时代高质量发展-2-市场预测
R language and machine learning
. Net core learning journey
Can string be changed?
软件调试测试的十大重要基本准则
DETR3D 多2d图片3D检测框架
(15) Blender source code analysis flash window display menu function
Restful API interface design standards and specifications
High performance computing center roce overview
MapReduce执行原理记录
[Flink] Flink batch mode map side data aggregation normalizedkeysorter
Contains an object field at offset position
Link monitoring pinpoint
Conditional variables for thread synchronization
Camera-CreateCaptureSession
Binary search method