wpf中, 使用MultiBinding 和转换器实现多条件筛选

html 复制代码
<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">
                                        <!--<Setter Property="IsEnabled" Value="{Binding Age, Converter={StaticResource SingleParamConverter}}" />-->
                                        <Setter Property="IsEnabled">
                                            <Setter.Value>
                                                <MultiBinding Converter="{StaticResource MultiParamConverter}">
                                                    <Binding Path="Age"/>
                                                    <Binding Path="Id"/>
                                                </MultiBinding>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                </Button.Style>
                            </Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>
csharp 复制代码
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();
        }
    }
}
csharp 复制代码
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);
        }
    }
}

这样就实现了根据年龄, Id 筛选按钮的enable了

相关推荐
月落.10 小时前
WPF的<ContentControl>控件
wpf
就是有点傻10 小时前
WPF中的依赖属性
开发语言·wpf
wangnaisheng10 小时前
【WPF】把一个Window放在左上角/右上角顶格显示
wpf
WineMonk10 小时前
.NET WPF CommunityToolkit.Mvvm框架
.net·wpf·mvvm
月落.10 小时前
WPF中的INotifyPropertyChanged接口
wpf
界面开发小八哥10 小时前
界面控件DevExpress WPF中文教程:Data Grid——卡片视图设置
.net·wpf·界面控件·devexpress·ui开发
平凡シンプル10 小时前
WPF 打包
wpf
VickyJames10 小时前
基于XAML框架和跨平台项目架构设计的深入技术分析
wpf·开源分享·unoplatform·winui3·项目架构
冷眼Σ(-᷅_-᷄๑)13 小时前
WPF缩放动画和平移动画叠加后会发生什么?
wpf·动画
△曉風殘月〆15 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm