zoukankan      html  css  js  c++  java
  • wpf convert

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

     xmlns:local="clr-namespace:KTVClient.Modules.Main.VMMBase.PublicViewModel;assembly=KTVClient.Modules.Main.VMMBase"     >

        <local:BoolVisible x:Key="converter" />

     Visibility="{Binding IsShowing, Converter={StaticResource ResourceKey=converter}">

    public class BoolVisible : IValueConverter     {

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)      

       {           

      bool visible = (bool) value;        

         if (visible != null)          

           return visible ? "Visible" : "Collapsed";       

          else                 return "Collapsed";                

        }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)         {         

        return null;    

         }  

       }

    或是用  <BooleanToVisibilityConverter x:Key="boolToVis"/> 试试

    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
        }
    }




    转载http://www.cnblogs.com/qiurideyun/archive/2013/02/15/2912929.html
     
     
     
     
     
     
     
  • 相关阅读:
    mysql导入导出数据
    Linux符号连接的层数过多
    win10下docker安装和配置镜像仓库
    PHP资源列表(转)
    php中正则案例分析
    基于CSS3自定义美化复选框Checkbox组合
    基于HTML5 Canvas粒子效果文字动画特效
    基于jQuery商品分类选择提交表单代码
    基于jquery右侧悬浮加入购物车代码
    基于jquery带时间轴的图片轮播切换代码
  • 原文地址:https://www.cnblogs.com/aggierwyp/p/wpfconvert.html
Copyright © 2011-2022 走看看