首页 > 其他分享 >WPF 装饰器 、 转换器 、行为

WPF 装饰器 、 转换器 、行为

时间:2023-12-26 14:34:07浏览次数:35  
标签:Convert object System value values 转换器 WPF 装饰

十年河东,十年河西,莫欺少年穷

学无止境,精益求精

行为请参考:WPF 行为 

装饰器参考:

 wpf 转换器

详情参考:

单值转换器需继承自 IValueConverter

public class MyNumberConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value!=null&& System.Convert.ToInt32(value) % 2 == 0)
            {
                return "偶数";
            }
            return "奇数";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

多值转换器需继承自IMultiValueConverter

public class MyColorConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values != null && values.Length > 2)
            {
                var R = System.Convert.ToByte(values[0]);
                var G = System.Convert.ToByte(values[1]);
                var B = System.Convert.ToByte(values[2]);
                System.Windows.Media.Color color = System.Windows.Media.Color.FromRgb(R,G,B);
                 return new SolidColorBrush(color);
            }
            return null;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

使用转换器

xaml 如下:

<Window x:Class="WpfApp9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:MyConverter="clr-namespace:WpfApp9.Converter"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="600">
    <Window.Resources>
        <MyConverter:MyNumberConverter x:Key="MyNumber"/>
        <MyConverter:MyColorConverter x:Key="MyColor"/>
         
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0">
            <TextBox x:Name="txt" Text="0" Width="100" Height="40" FontFamily="50"/>
            <TextBlock Text="{Binding ElementName=txt,Path=Text,Converter={StaticResource MyNumber}}" FontSize="50" />
        </StackPanel>

        <StackPanel Grid.Column="1">
            <Slider x:Name="Rcolor" Maximum="255" Minimum="0" Value="0" Width="200" Margin="10"/>
            <Slider x:Name="Gcolor" Maximum="255" Minimum="0" Value="0" Width="200" Margin="10"/>
            <Slider x:Name="Bcolor" Maximum="255" Minimum="0" Value="0" Width="200" Margin="10"/>
            <Path VerticalAlignment="Center"   Stroke="Black"  StrokeThickness="3">
                <Path.Data>
                    <EllipseGeometry Center="80,80" RadiusX="60" RadiusY="60"/>
                </Path.Data>
                <Path.Fill>
                    <MultiBinding Converter="{StaticResource MyColor}">
                        <Binding ElementName="Rcolor" Path="Value"></Binding>
                        <Binding ElementName="Gcolor" Path="Value"></Binding>
                        <Binding ElementName="Bcolor" Path="Value"></Binding>
                    </MultiBinding>
                </Path.Fill>
            </Path>
        </StackPanel>
    </Grid>
</Window>

View Code

效果:

WPF 装饰器 、 转换器 、行为_System

 注:需要在资源中引入定义的转换器

<Window.Resources>
        <MyConverter:MyNumberConverter x:Key="MyNumber"/>
        <MyConverter:MyColorConverter x:Key="MyColor"/>
         
    </Window.Resources>

使用如下:

<TextBlock Text="{Binding ElementName=txt,Path=Text,Converter={StaticResource MyNumber}}" FontSize="50" />

 



标签:Convert,object,System,value,values,转换器,WPF,装饰
From: https://blog.51cto.com/u_15316082/8983337

相关文章

  • wpf + MaterialDesign + Prism8 实现导航功能
    十年河东,十年河西,莫欺少年穷学无止境,精益求精实现的效果: 1、初始化Prism 1.1、项目引入如下包 1.2、按照Prism规则,项目中创建如下文件夹 Prism规则:必须将窗体放入Views文件夹中,窗体名称必须以View结尾,必须将数据上下文放入ViewModels文件夹中,上下文类必须以Model结尾另外两个......
  • WPF+SqlSugar+MVVM实现增删改查(二)
    这相对于上一版本的升级版如果不理解看请看第一版:WPF+SqlSugar+MVVM实现增删改查-六子12138-博客园(cnblogs.com)ViewModels代码1usingEntitys;2usingSqlSugar;3usingSystem;4usingSystem.Collections.Generic;5usingSystem.Collections.Object......
  • wpf + MaterialDesign + Prism8 + DataGrid 实现表格内数据编辑,下拉
    十年河东,十年河西,莫欺少年穷学无止境,精益求精效果如下: xaml如下:<UserControlx:Class="WpfApp.UserControls.MemoView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/w......
  • WPF Halcon机器视觉和运动控制软件通用框架,插件式开发,开箱即用 仅供学习!
    点我下载,仅供个人学习使用参考easyvision开发,集成几十个软件算子此版本以添加ui设计器。具体功能如上所示,可以自定义变量,写c#脚本,自定义流程,包含了halcon脚本和封装的算子,可自定义ui,通过插件形式开发很方便拓展自己的功能。......
  • wpf + MaterialDesign + Prism8 + DataGrid 实现表格数据+分页
    十年河东,十年河西,莫欺少年穷学完止境,精益求精1、不分页,带有排序功能(每个字段都可以排序) xaml如下:<UserControlx:Class="WpfApp.UserControls.UserView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http:......
  • 常用的10个Python装饰器
    装饰器(Decorators)是Python中一种强大而灵活的功能,用于修改或增强函数或类的行为。装饰器本质上是一个函数,它接受另一个函数或类作为参数,并返回一个新的函数或类。它们通常用于在不修改原始代码的情况下添加额外的功能或功能。装饰器的语法使用@符号,将装饰器应用于目标函数或类。下......
  • 深入理解WPF中的Dispatcher:优化UI操作的关键
     概述:Dispatcher是WPF中用于协调UI线程和非UI线程操作的关键类,通过消息循环机制确保UI元素的安全更新。常见用途包括异步任务中的UI更新和定时器操作。在实践中,需注意避免UI线程阻塞、死锁,并使用CheckAccess方法确保在正确的线程上执行操作。这有助于提升应用程序的性能和用户......
  • 使用MVVM Toolkit简化WPF开发
    最近.NET8的WPF推出了 WPFFileDialog改进,这样无需再引用 Win32 命名空间就可以实现文件夹的选择与存储了,算是一个很方便的改进了。顺手写了一个小的WPF程序,在使用 Model-View-ViewModel(MVVM) 模式的时候,我不想使用 Prism 等重量级的框架,找了一个轻量级的MVVMCo......
  • 循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发
    https://www.cnblogs.com/wuhuacong/tag/WPF/ 在我们的SqlSugar的开发框架中,整合了Winform端、Vue3+ElementPlus的前端、以及基于UniApp+Vue+ThorUI的移动前端几个前端处理,基本上覆盖了我们日常的应用模式了,本篇随笔进一步介绍前端应用的领域,研究集成WPF的应用端,循序渐进介绍基......
  • WPF MvvmToolkit入门
    最新.net6wpfMVVMToolkit8.0工程搭建。MVVMToolkit是一个轻量级MVVM框架,在框架下我们第一个要做的就是搞清在此框架下的一些常规操作:属性绑定和通知,命令绑定,消息传递。搞懂这些处理流程,然后就可以写自己业务的逻辑。1.安装mvvmtoolkit1.1Nuget下载CommunityToolkit.Mvvm......