美文网首页
WPF 值转换

WPF 值转换

作者: Ritchie_Li | 来源:发表于2022-06-25 23:37 被阅读0次

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设计绑定转换类

<DockPanel Grid.Row="0" Margin="3">

  <TextBlock DockPanel.Dock="Left" VerticalAlignment="Center">License expired</TextBlock>

  <TextBlock DockPanel.Dock="Left" VerticalAlignment="Center" Foreground="Green" FontWeight="Black" Text="{Binding LicenseExpired}" Padding="3"/>

  <Button Margin="3" Content="Start" x:Name="btnStart" 

    IsEnabled="{Binding LicenseExpired, Converter={StaticResource bool2Inverter}}"/>

</DockPanel>

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">

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

        <TextBlock Text="BorderThickness:"/>

        <TextBlock Text="{Binding ElementName=bd,Path=BorderThickness,UpdateSourceTrigger=PropertyChanged}"

        FontSize="{Binding BoolProperty ,Converter={StaticResource bool2Int},Mode=OneWayToSource}"/>

    </StackPanel>

</Border>

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"/>

<TextBox Text="{Binding Grade,Converter={StaticResource Int2Str},ConverterParameter=45}"></TextBox>

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"/>

<TextBox Grid.Column="1" Name="tb_input" Padding="5 0" Margin="5"/>

<Button Grid.Column="2" Content="Submit" IsEnabled="{Binding ElementName=tb_input, Path=Text, Converter={StaticResource str2Bool}}" />

输入字符串为空时,Button 不可用,字符串输入不为空时,Enable

相关文章

网友评论

      本文标题:WPF 值转换

      本文链接:https://www.haomeiwen.com/subject/xxywvrtx.html