• WPF中的多重绑定


    MultiBinding 将会给后端传回一个数组, 其顺序为绑定的顺序. 例如:

            <DataGrid
                Margin="10"
                AutoGenerateColumns="False"
                ItemsSource="{Binding Stu}">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Id}" Header="Id" />
                    <DataGridTextColumn Binding="{Binding Name}" Header="Name" />
                    <DataGridTextColumn Binding="{Binding Age}" Header="Age" />
                    <DataGridTextColumn Binding="{Binding Description}" Header="Description" />
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button
                                    Width="60"
                                    HorizontalAlignment="Center"
                                    Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.MyButtonCommand}"
                                    CommandParameter="{Binding}"
                                    Content="申请">
                                    <Button.Style>
                                        <Style TargetType="Button">
                                            
                                            "IsEnabled">
                                                
                                                    "{StaticResource MultiParamConverter}">
                                                        "Age"/>
                                                        "Id"/>
                                                    
                                                
                                            
                                        Style>
                                    Button.Style>
                                Button>
                            DataTemplate>
                        DataGridTemplateColumn.CellTemplate>
                    DataGridTemplateColumn>
                DataGrid.Columns>
            DataGrid>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    在这里的 Button 的isEnabled属性用了多重绑定给converter, 用来筛选条件

                                            <Setter Property="IsEnabled">
                                                <Setter.Value>
                                                    <MultiBinding Converter="{StaticResource MultiParamConverter}">
                                                        <Binding Path="Age"/>
                                                        <Binding Path="Id"/>
                                                    MultiBinding>
                                                Setter.Value>
                                            Setter>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    这时 后端转换器为:

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Data;
    
    namespace NavTest.Components
    {
        public class MultiParamConverter : IMultiValueConverter
        {
            public object Convert(
                object[] values,
                Type targetType,
                object parameter,
                CultureInfo culture
            )
            {
                int age;
                int id;
    
                if (values == null)
                {
                    return true;
                }
    
                int.TryParse(values[0].ToString(), out age);
                int.TryParse(values[1].ToString(), out id);
                if (age > 1 && id > 5)
                {
                    return true;
                }
                return false;
            }
    
            public object[] ConvertBack(
                object value,
                Type[] targetTypes,
                object parameter,
                CultureInfo culture
            )
            {
                throw new NotImplementedException();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    效果:

    在这里插入图片描述
    完整代码:

    view:

    <UserControl
        x:Class="NavTest.Views.Page5"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cv="clr-namespace:NavTest.Components"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:hc="https://handyorg.github.io/handycontrol"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:local="clr-namespace:NavTest.Views"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:mv="clr-namespace:NavTest.ViewModels"
        d:DataContext="{d:DesignInstance mv:Page5ViewModel}"
        d:DesignHeight="450"
        d:DesignWidth="800"
        mc:Ignorable="d">
        <UserControl.Resources>
            <cv:SingleParamConverter x:Key="SingleParamConverter" />
            <cv:MultiParamConverter x:Key="MultiParamConverter" />
        UserControl.Resources>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            Grid.RowDefinitions>
            <DataGrid
                Margin="10"
                AutoGenerateColumns="False"
                ItemsSource="{Binding Stu}">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Id}" Header="Id" />
                    <DataGridTextColumn Binding="{Binding Name}" Header="Name" />
                    <DataGridTextColumn Binding="{Binding Age}" Header="Age" />
                    <DataGridTextColumn Binding="{Binding Description}" Header="Description" />
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button
                                    Width="60"
                                    HorizontalAlignment="Center"
                                    Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.MyButtonCommand}"
                                    CommandParameter="{Binding}"
                                    Content="申请">
                                    <Button.Style>
                                        <Style TargetType="Button">
                                            
                                            "IsEnabled">
                                                
                                                    "{StaticResource MultiParamConverter}">
                                                        "Age"/>
                                                        "Id"/>
                                                    
                                                
                                            
                                        Style>
                                    Button.Style>
                                Button>
                            DataTemplate>
                        DataGridTemplateColumn.CellTemplate>
                    DataGridTemplateColumn>
                DataGrid.Columns>
            DataGrid>
        Grid>
    UserControl>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    viewModel:

    using CommunityToolkit.Mvvm.ComponentModel;
    using CommunityToolkit.Mvvm.Input;
    using NavTest.Eneities;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    
    namespace NavTest.ViewModels
    {
        public partial class Page5ViewModel:ObservableObject
        {
            public Page5ViewModel()
            {
                for (int i = 0; i < 10; i++)
                {
                    Stu.Add(new()
                    {
                        Id = i + 2,
                        Age = $"{i}",
                        Name = $"Name{i}",
                        Description = $"Description{i}"
                    });
                }
            }
    
            [ObservableProperty]
            private ObservableCollection<Student> stu = new();
    
    
            [RelayCommand]
            public void MyButton(Student s)
            {
                MessageBox.Show(s.Name);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    转换器:

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Data;
    
    namespace NavTest.Components
    {
        public class MultiParamConverter : IMultiValueConverter
        {
            public object Convert(
                object[] values,
                Type targetType,
                object parameter,
                CultureInfo culture
            )
            {
                int age;
                int id;
    
                if (values == null)
                {
                    return true;
                }
    
                int.TryParse(values[0].ToString(), out age);
                int.TryParse(values[1].ToString(), out id);
                if (age > 1 && id > 5)
                {
                    return true;
                }
                return false;
            }
    
            public object[] ConvertBack(
                object value,
                Type[] targetTypes,
                object parameter,
                CultureInfo culture
            )
            {
                throw new NotImplementedException();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Data;
    
    namespace NavTest.Components
    {
        public class SingleParamConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (value == null)
                {
                    return true;
                }
                int age;
                int.TryParse(value.ToString(), out age);
                if (age > 5)
                {
                    return true;
                }
                return false;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
  • 相关阅读:
    【Redis】Redis事务:原子性与回滚的真相揭秘
    Vue3 相较 Vue2 做的重大更新
    通信网络从4G升级到5G,核心网融合至关重要
    干货 | 5719个字详解低代码在某银行&券商的实践
    Hadoop核心机制详细解析
    近期面试问题答得不好的知识
    6-5,web3浏览器链接区块链(react+区块链实战)
    【浅尝C++】STL第三弹=>list常用接口使用示例/list底层结构探索/list模拟实现代码详解
    C++第一篇--关键字以及命名空间
    伙伴云戴志康:如何利用低代码提升研发和IT效能
  • 原文地址:https://blog.csdn.net/helldoger/article/details/133789783