当前位置:网站首页>WPF 值转换
WPF 值转换
2022-06-26 03:50:00 【flysh05】
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
边栏推荐
- 【LOJ#6718】九个太阳「弱」化版(循环卷积,任意模数NTT)
- Solve the problem that the uniapp plug-in Robin editor reports an error when setting the font color and background color
- 2022.6.25-----leetcode.剑指offer.091
- Restful API interface design standards and specifications
- 面了个字节拿25k出来的测试,算是真正见识到了基础的天花板
- Oracle技术分享 oracle 19.14升级19.15
- mysql存储过程
- Uni app custom drop-down selection list
- 763. dividing alphabetic intervals
- Solve the problem that the input box is blocked by the pop-up keyboard under the WebView transparent status bar
猜你喜欢
MapReduce执行原理记录
判断两个集合的相同值 ||不同值
MySQL高级部分( 四: 锁机制、SQL优化 )
ABP framework Practice Series (III) - domain layer in depth
Uni app Baidu cloud realizes OCR ID card recognition
Open Camera异常分析(一)
C # knowledge structure
matplotlib折线图,文字显示,win10
商城风格也可以很多变,DIY 了解一下
The kotlin project is running normally and the R file cannot be found
随机推荐
Comparison of static methods and variables with instance methods and variables
mysql 常用语句
Camera-memory内存泄漏分析(三)
Is the compass app regular? Is it safe or not
WebRTC系列-网络传输之6-Connections裁剪
816. fuzzy coordinates
The style of the mall can also change a lot. DIY can learn about it
链路监控 pinpoint
bubble sort
等保备案是等保测评吗?两者是什么关系?
Alibaba cloud function computing service one click to build Z-blog personal blog
评价——层次分析
Communication mode between processes
虚拟化是什么意思?包含哪些技术?与私有云有什么区别?
Camera-CreateCaptureSession
Slide the menu of uni app custom components left and right and click switch to select and display in the middle
I/O 虚拟化技术 — VFIO
Webrtc series - 7-ice supplement of network transmission preference and priority
Uni app Baidu cloud realizes OCR ID card recognition
Binary search