WPF 多路绑定、值转换器ValueConvert、数据校验

值转换器 valueconvert

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
使用ValueConverter需要实现IValueConverter接口,其内部有两个方法,Convert和ConvertBack。我们在使用Binding绑定数据的时候,当遇到源属性和目标控件需要的类型不一致的,就可以使用ValueConverter,它就相当于一个桥,当数据从源到目标控件时,需要走Convert方法,我们在这个方法里边就可以自定义转换逻辑,当数据从目标控件到源时,需要走ConvertBack方法,我们可以在这里边自定义回转逻辑。

多路绑定

在这里插入图片描述

<Window x:Class="DEMO2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DEMO2" xmlns:sys="http://schemas.microsoft.com/winfx/2009/xaml"
        mc:Ignorable="d"      
   
       Title="333"
        Height="450" Width="800"  >
    <Window.Resources>
        <ResourceDictionary>
            <local:HasValuesMultiConvert x:Key="HAS"></local:HasValuesMultiConvert>
        </ResourceDictionary>
    </Window.Resources>
    <Grid MouseDown="Grid_MouseDown" Background="Green">
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox x:Name="txt1" Grid.Row="0"></TextBox>
        <TextBox x:Name="txt2" Grid.Row="1"></TextBox>
        <Button Grid.Row="2"  Content="登录" FontSize="20">
            <Button.IsEnabled>
                <MultiBinding Converter="{StaticResource HAS}" UpdateSourceTrigger="PropertyChanged">
                    <Binding Path="Text" Source="{x:Reference txt1}"></Binding>
                    <Binding Path="Text" Source="{x:Reference txt2}"></Binding>
                </MultiBinding>
            </Button.IsEnabled>
        </Button>
    </Grid>
</Window>

在这里插入图片描述

数据校验

在这里插入图片描述
在这里插入图片描述

相关推荐

  1. WPF数据

    2024-03-29 05:14:06       36 阅读
  2. WPF中将个函数返回分别至界面控件

    2024-03-29 05:14:06       37 阅读
  3. WPF —— Menu数据实例

    2024-03-29 05:14:06       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-29 05:14:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-29 05:14:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-29 05:14:06       82 阅读
  4. Python语言-面向对象

    2024-03-29 05:14:06       91 阅读

热门阅读

  1. NatCat使用说明

    2024-03-29 05:14:06       39 阅读
  2. Yarn的安装和使用

    2024-03-29 05:14:06       42 阅读
  3. VUE3——setup介绍

    2024-03-29 05:14:06       38 阅读
  4. 【Kotlin】List、Set、Map简介

    2024-03-29 05:14:06       38 阅读
  5. js的apply、call、bind

    2024-03-29 05:14:06       39 阅读
  6. python解压RAR文件

    2024-03-29 05:14:06       37 阅读
  7. 大前端-postcss安装使用指南

    2024-03-29 05:14:06       45 阅读
  8. ubuntu18.04找不到网络适配器,无法连接网络

    2024-03-29 05:14:06       42 阅读
  9. CUDA从入门到放弃(七):流( Streams)

    2024-03-29 05:14:06       45 阅读
  10. SHELL(01)

    2024-03-29 05:14:06       37 阅读