• 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
  • 相关阅读:
    微信小程序│ 游戏开发 │连连看游戏
    Greetings(状压DP,枚举子集转移)
    Docker in docker 实现
    gitlab的使用方法,详解gitlab操作
    【JavaEE】MyBaits(注解方式实现映射)
    利用OPNET进行网络仿真时网络层协议(以QoS为例)的使用、配置及注意点
    【数模】典型相关分析
    nginx部署web项目(跟着搞不出来,来砍我)
    springcloud:3.1介绍雪崩和Resilience4j
    了解基于Elasticsearch 的站内搜索,及其替代方案
  • 原文地址:https://blog.csdn.net/helldoger/article/details/133789783