在写程序中经常遇到这样的情况:本示例代码下载地址
点一个按钮,另外一个按钮或者几个被禁用掉,点另外的按钮,被禁用的按钮又恢复正常,平常的处理方法是在按钮点击事件中直接对其他按钮的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,
}
}