zoukankan      html  css  js  c++  java
  • [原创]Prism Command 和 IsEnabled 的冲突解决方法

    在开发Silverlight项目中,学习使用MVVM ,在Button的click上想使用Prism下的Click Command方法来处理Click事件,发现当用到Click事件绑定的时候,
    绑定IsEnabled的属性不起作用了。
    XAML代码如下
    Code

    C#代码如下
        public class MainPageViewModel : INotifyPropertyChanged  
        {
            
    public MainPageViewModel()
            {
                BTNEnabledCommand 
    = new DelegateCommand<object>(OnEnabledCommand);
                BTNClickCommand 
    = new DelegateCommand<object>(OnClickCommand);
            }
            
    private void OnEnabledCommand(object arg)
            {
                IsNewEnabled 
    = !IsNewEnabled;                        
            }
            
    private void OnClickCommand(object arg)
            {
                MessageBox.Show(
    "Hello Wingfay");
            }
            
    public ICommand BTNClickCommand { getprivate set; }
            
    public ICommand BTNEnabledCommand { getprivate set; }
            
    private bool _IsNewEnabled;
            
    public bool IsNewEnabled
            {
                
    get
                {
                    
    return _IsNewEnabled;
                }
                
    set
                {
                    _IsNewEnabled 
    = value;
                    OnPropertyChanged(
    "IsNewEnabled");
                }
            }

           ....
        }
    郁闷死我了,在网上找资料发现了这个
    引用

    The Click.Command attached property provided by Composite Application Guidance for WPF & Silverlight requires you to specify an ICommand (generally using DelegateCommand class). In that command you define an Execute and CanExecute method. The CAL relies on this latter method to check if the command can be executed and sets the control's IsEnabled property to the value returned by the CanExecute method.

    So, when you use CAL's Click attached property to define commands, the IsEnabled property is set at runtime by the command behavior (more precisely in the UpdateEnabledState method of the CommandBehaviorBase class) which is executed after the isEnabled set in XAML.

     

          protected virtual void UpdateEnabledState()

          {

                ...

                if (this.Command != null)

                {

                    TargetObject.IsEnabled = this.Command.CanExecute(this.CommandParameter);

                }
          }

     

    You can use the RaiseCanExecuteChanged method from the DelegateCommand class to reevalute the CanExecute method and that will update the isEnabled property of all attached controls.

    以上是Prism开发人员的回答
    然后看了下Prism关于Command的源代码 终于明白了点
    该后的代码如下
    XAML
        <UserControl.Resources>
            
    <Local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
            
    <Local:MainPageViewModel x:Key="MainPageViewModelDataSource" d:IsDataSource="True"/>
     
        
    </UserControl.Resources>  
        
    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MainPageViewModelDataSource}}">
          
    <Button Height="36" HorizontalAlignment="Left" Margin="128,126,0,0" VerticalAlignment="Top" Width="58" Content="New" 
             Visibility
    ="{Binding IsNewVisible, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}"
               commands:Click.Command
    ="{Binding BTNClickCommand}"
                
    />
          
    <Button Height="36" Margin="292,126,273,0" VerticalAlignment="Top" Content="Enabled" commands:Click.Command="{Binding BTNEnabledCommand}"/>


      
    </Grid>

    C#代码

    Code
    *转载请注明来自哪里就行。
  • 相关阅读:
    js获取当前页面url网址等信息
    jQuery 复选框全选/取消全选/反选
    js获取日期实例之昨天今天和明天、后天
    mysql不重启修改参数变量
    php 读取功能分割大文件实例详解
    php批量删除数据库下指定前缀的表
    PHP遍历目录返回统计目录大小实例
    PHP二维数组如何根据某个字段排序
    PHP 如何获取二维数组中某个key的集合(高性能查找)
    jQuery 隐藏与显示 input 默认值
  • 原文地址:https://www.cnblogs.com/wingfay/p/1544814.html
Copyright © 2011-2022 走看看