微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

首次运行CanExecute时CommandParameter返回null

如何解决首次运行CanExecute时CommandParameter返回null

我有以下XAML:

 <Button Margin="222,256,102,19" Content="Add Customer" Command="{Binding AddCustomers}">
            <Button.CommandParameter>
                <MultiBinding Converter="{StaticResource MyMultiConverter}">
                    <Binding ElementName="Name" Path="SelectedItem"></Binding>
                    <Binding ElementName="Address" Path="SelectedValue"></Binding>
                </MultiBinding>
            </Button.CommandParameter>
        </Button>

CommandParameter 总是在第一次 CanExecute 运行时返回null。这意味着在我的viewmodel的 CanExecute 方法中,参数( CustomerInfo )总是在该方法第一次执行时返回null,这会导致异常。

 private bool CanAddCustomers(object CustomerInfo)
        {
            var CustomerInfotocheck= (object[])CustomerInfo; // exception here the first time it runs because CustomerInfo is null. The second time this method runs,it returns the values I passed in CommandParameter

            ....
        }

转换器方法在CanExecute( CanAddCustomers )首次运行后运行,然后返回我在 CommandParameter 中传递的值。这不会在其他奇怪的按钮命令上发生。这有什么用?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。