delegateCommand.cs:
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace SelfPro.Practice.SilverlightTest1.Common { public class DelegateCommand : ICommand { private readonly Func<bool> _canExecute; private readonly Action _execute; public event EventHandler CanExecuteChanged; public DelegateCommand(Action execute) : this(execute,null) { } public DelegateCommand(Action execute,Func<bool> canExecute) { _execute = execute; _canExecute = canExecute; } public bool CanExecute(object o) { if (_canExecute == null) { return true; } return _canExecute(); } public void Execute(object o) { _execute(); } public void RaiseCanExecuteChanged() { if (CanExecuteChanged != null) { CanExecuteChanged(this,EventArgs.Empty); } } } }
NotificationObject.cs:
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.ComponentModel; namespace SelfPro.Practice.SilverlightTest1.Common { public abstract class NotificationObject : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { handler(this,new PropertyChangedEventArgs(propertyName)); } } protected void RaisePropertyChanged(params string[] propertyNames) { if (propertyNames == null) throw new ArgumentNullException("propertyNames"); foreach (var name in propertyNames) { this.RaisePropertyChanged(name); } } } }
binding helper.cs:
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace SelfPro.Practice.SilverlightTest1.Common { public class BindingHelper { public static bool GetUpdateSourceOnChange (DependencyObject obj) { return (bool)obj.GetValue(UpdateSourceOnChangeProperty); } public static void SetUpdateSourceOnChange (DependencyObject obj,bool value) { obj.SetValue(UpdateSourceOnChangeProperty,value); } // Using a DependencyProperty as the backing store for … public static readonly DependencyProperty UpdateSourceOnChangeProperty = DependencyProperty.Registerattached( "UpdateSourceOnChange",typeof(bool),typeof(BindingHelper),new PropertyMetadata(false,OnPropertyChanged)); public static void OnPropertyChanged (DependencyObject obj,DependencyPropertyChangedEventArgs e) { var txt = obj as TextBox; if (txt == null) return; if ((bool)e.NewValue) { txt.TextChanged += OnTextChanged; } else { txt.TextChanged -= OnTextChanged; } } public static void OnTextChanged(object sender,TextChangedEventArgs e) { var txt = sender as TextBox; if (txt == null) return; var be = txt.GetBindingExpression(TextBox.TextProperty); if (be != null) { be.UpdateSource(); } } } }
mainwindowviewmodel.cs:
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using SelfPro.Practice.SilverlightTest1.Common; namespace SelfPro.Practice.SilverlightTest1.viewmodel { public class MainPageviewmodel:NotificationObject { private string inputStr; public string InputStr { get { return inputStr; } set { inputStr = value; RaisePropertyChanged("InputStr"); } } public DelegateCommand CmdRun { get; private set; } public MainPageviewmodel() { CmdRun = new DelegateCommand(new Action(Run),new Func<bool>(CanRun)); this.PropertyChanged += (s,e) => { CmdRun.RaiseCanExecuteChanged(); }; } private void Run() { InputStr = "aaa"; } private bool CanRun() { if (string.IsNullOrEmpty(inputStr)) return false; return inputStr.Equals("aaaaa"); } } }
mainwindow.xaml:
<UserControl x:Class="SelfPro.Practice.SilverlightTest1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:SelfPro.Practice.SilverlightTest1.viewmodel" xmlns:cm="clr-namespace:SelfPro.Practice.SilverlightTest1.Common" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" > <UserControl.DataContext> <vm:MainPageviewmodel></vm:MainPageviewmodel> </UserControl.DataContext> <UserControl.Resources> <Style targettype="Button"> <Setter Property="Background" Value="SkyBlue" /> <Setter Property="Width" Value="80" /> <Setter Property="Height" Value="24" /> </Style> </UserControl.Resources> <Border BorderThickness="2" Margin="10" CornerRadius="10" Height="200"> <Border.Background> <LinearGradientBrush> <GradientStop Color="Green" Offset="0" /> <GradientStop Color="Wheat" Offset="1" /> </LinearGradientBrush> </Border.Background> <Border.BorderBrush> <LinearGradientBrush> <GradientStop Color="SkyBlue" Offset="1" /> <GradientStop Color="SkyBlue" Offset="0" /> </LinearGradientBrush> </Border.BorderBrush> <StackPanel> <Button Width="100" Height="25" Content="click me" Command="{Binding CmdRun}" /> <TextBox Text="{Binding InputStr,Mode=TwoWay}" Width="200" cm:BindingHelper.UpdateSourceOnChange="True" /> </StackPanel> </Border> </UserControl>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。