zoukankan      html  css  js  c++  java
  • 菲佣WPF——5(Convert瞎想)

    Convert在WPF页面显示中,使用比较多。

    今天就来瞎想下Convert

    1.先来个最常用的 bool 转 Visible

    XAML代码:

    <Window x:Class="WpfApplication4.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:local="clr-namespace:WpfApplication4"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:BoolVisible x:Key="converter"/>
        </Window.Resources>
        <Grid>
            <Button x:Name="btnDemo" Width="100" Height="50" Visibility="{Binding IsVisible,Converter={StaticResource ResourceKey=converter}}" Command="{Binding ClickCommand}"/>
        </Grid>
    </Window>

    ViewModel代码:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    
    namespace WpfApplication4
    {
        public class MainWindowsViewModel:NotifyObject
        {
            #region << Property >>
            public bool IsVisible
            {
                get { return GET(() => IsVisible); }
                set { SET(() => IsVisible, value); }
            }
    
            public ICommand ClickCommand { get; set; }
            #endregion
            #region << Constructor >>
            public MainWindowsViewModel()
            {
                IsVisible = true;
                ClickCommand = new DeletegateCommand(Click);
            }
            #endregion
    
            #region << Method >>
            public void Click()
            {
                IsVisible = false;           
            }
            #endregion
    
        }
    }

    Converter的实现代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Data;
    
    namespace WpfApplication4
    {
        public class BoolVisible:IValueConverter
        {
            #region << Method >>
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                bool visible = (bool)value;
    
                if (visible != null)
                    return visible ? "Visible" : "Hidden";
                else
                    return "Hidden";
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return null;
            }
            #endregion
        }
    }
  • 相关阅读:
    django基础知识之模型查询:
    django基础知识之定义模型:
    django基础知识之ORM简介:
    django基础知识之认识MVT MVC:
    解决ImportError: libmysqlclient_r.so.16: cannot open shared object file-乾颐堂
    python 多继承详解-乾颐堂
    一步步来用C语言来写python扩展-乾颐堂
    nltk 之 snowball 提取词干-乾颐堂
    Python 执行js的2种解决方案-乾颐堂
    常用的 Python 调试工具,Python开发必读-乾颐堂
  • 原文地址:https://www.cnblogs.com/qiurideyun/p/2912929.html
Copyright © 2011-2022 走看看