惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - wingfay

【转载】解决 安装或卸载软件时报错Error 1001 的问题 Win7下做一个Kubernetes的NetCore项目Demo win7 升级Power Shell到4.0 .net Cache 需要注意的地方 SQL 字段修改 Powser Design 16.5 导入Mysql数据库的bug Druid中配置双数据库 ActiveMq Windows 配置优化 Eclipse使用Maven创建Web时错误:Could not resolve archetype 更新VS2017会出现“请更新 Visual Studio 安装程序 再继续” 提示 ipv6过审函数 转.iPhone开发网站、论坛、博客 NSLog的使用 Could not load type System.Security.Authentication..... [ZT]JScript下Array对象的性能问题 获取地区的Resolve Weather Location ID Using .NET C# LDAP Library(Novell.Directory.Ldap) WINDOWS下搭建LDAP服务器 [转帖]LDAP Schema的概念和基本要素
[原创]Prism Command 和 IsEnabled 的冲突解决方法
wingfay · 2009-08-13 · via 博客园 - wingfay

在开发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

*转载请注明来自哪里就行。