当前位置:网站首页>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

原网站

版权声明
本文为[flysh05]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260350194701.html