WPF中的值绑定转换

在写程序中经常遇到这样的情况:本示例代码下载地址
点一个按钮,另外一个按钮或者几个被禁用掉,点另外的按钮,被禁用的按钮又恢复正常,平常的处理方法是在按钮点击事件中直接对其他按钮的IsEnable属性赋值来处理。但有时候复杂的情况导致我们常常担心这样的写法过于复杂,就几个按钮和一些控件的状态,我们能不能只管写逻辑代码,不去管这么多控件的ENABLE和DISABNLE呢?使用WPF中的绑定值转换可以达到这样的效果,举例说明:
这里写图片描述
如图中的三个按钮
点击修改:修改按钮:disable,取消按钮:enable,保存按钮:enable
点击取消:修改按钮:enable,取消按钮:disable,保存按钮:disable
点击保存:修改按钮:enable,取消按钮:disable,保存按钮:disable
利用WPF中的值绑定转换:转换部分的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace WPF_BindingTest
{
   public class OperationTypeConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool result = false;
            if (parameter.ToString().Equals("Save"))//是save按钮
            {
                switch (value.ToString())
                {
                    case "Mod": result = true; break;
                    case "None": result = false; break;
                    default: result = false;
                        break;
                }
            }
            else if (parameter.ToString().Equals("Cancel"))//取消按钮
            {
                switch (value.ToString())
                {
                    case "Mod": result = true; break;
                    case "None": result = false; break;
                    default: result = false;
                        break;
                }
            }
            else//修改按钮
            {
                switch (value.ToString())
                {
                    case "Mod": result = false; break;
                    case "None": result = true; break;
                    default: result = false;
                        break;
                }
            }
            return result;
        }

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

界面部分XAML代码如下:

<Window x:Class="WPF_BindingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPF_BindingTest"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:OperationTypeConverter x:Key="OpConverter"></local:OperationTypeConverter>
    </Window.Resources>
    <Grid>
        <Button Content="修改" Command="{Binding ModCommand}" IsEnabled="{Binding CurrentStatus, Converter={StaticResource OpConverter}, ConverterParameter=Mod}" Name="btnMod" HorizontalAlignment="Left" Margin="69,208,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="取消" Command="{Binding CancelCommand}" IsEnabled="{Binding CurrentStatus, Converter={StaticResource OpConverter}, ConverterParameter=Cancel}" Name="btnCancel" HorizontalAlignment="Left" Margin="176,208,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="保存" Command="{Binding SaveCommand}" IsEnabled="{Binding CurrentStatus, Converter={StaticResource OpConverter}, ConverterParameter=Save}" Name="btnSave" HorizontalAlignment="Left" Margin="286,208,0,0" VerticalAlignment="Top" Width="75"/>

    </Grid>
</Window>

ViewModel代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;

namespace WPF_BindingTest
{
   public class MainWindowVM:INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string name)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
        public MainWindowVM()
        {
            ModCommand = new CommonCommand(Modify);
            CancelCommand = new CommonCommand(Cancel);
            SaveCommand = new CommonCommand(Save);
        }
        #region 属性
        private OperationStatus currentStatus = OperationStatus.None;
        public OperationStatus CurrentStatus
        {
            get { return this.currentStatus; }
            set { this.currentStatus = value; OnPropertyChanged("CurrentStatus"); }
        }
        private ICommand modCommand;
        public ICommand ModCommand
        {
            get { return this.modCommand; }
            set { this.modCommand = value; OnPropertyChanged("ModCommand"); }
        }
        private ICommand cancelCommand;
        public ICommand CancelCommand
        {
            get { return this.cancelCommand; }
            set { this.cancelCommand = value; OnPropertyChanged("CancelCommand"); }
        }
        private ICommand saveCommand;
        public ICommand SaveCommand
        {
            get { return this.saveCommand; }
            set { this.saveCommand = value; OnPropertyChanged("SaveCommand"); }
        }
        #endregion
        #region 方法
        private void Modify(object para)
        {
            CurrentStatus = OperationStatus.Mod;
        }
        private void Cancel(object para)
        {
            CurrentStatus = OperationStatus.None;
        }
        private void Save(object para)
        {
            CurrentStatus = OperationStatus.None;
        }
        #endregion
    }
   public enum OperationStatus
   {
       None,
       Mod,     
   }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值