尽管受到绑定,WPF CommandParameter仍为null

时间:2017-10-03 00:35:56

标签: c# wpf mvvm commandparameter

我很难过这个。尽管绑定到具有值的属性,为什么命令参数将持续为空。

XAML

<UserControl x:Class="CatalogInterface.ctlMainButtonPanel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:CatalogInterface"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         x:Name="MainButtonPanel">

<UserControl.Resources>
    <local:MainButtonPanelViewModel x:Key="ViewModel" />
    <!--#region Button Style-->
    <!--#region  FocusVisual-->
    <!--#endregion FocusVisual-->
    <!--#region Button-->
    <!--#endregion Button-->
    <!--#endregion Button Style-->
</UserControl.Resources>

<Grid DataContext="{StaticResource ViewModel}">

<!--/////////////////////////////////////////////////////////////////////////////////-->
<!--////////This is the button that won't pass the CommandParameter//////////////////-->
<!--/////////////////////////////////////////////////////////////////////////////////-->
        <Button x:Name="cmdConvertToFBook" 
                CommandParameter="{Binding SelectedDocument}"
                Command="{Binding ConvertToFacebookCommand}" 
                Content="CONVERT TO FACEBOOK"
                HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  
                Grid.Column="0" Grid.Row="2" />
</UserControl>

查看模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace CatalogInterface
{
    class MainButtonPanelViewModel : ViewModelBase
    {
        private Messanger _messanger = Messanger.Set_Messanger();
    /////////////////////////////////////////////////////////////////////////////////
    ////////This is the property I'm binding my commandparameter to//////////////////
    /////////////////////////////////////////////////////////////////////////////////            
        private object _selectedDocument
        public object SelectedDocument
        {
            get { return _selectedDocument; }
            set { _selectedDocument = value; }
        }
        public ConvertToFacebookCommand ConvertToFacebookCommand { get; set; }

        public MainButtonPanelViewModel()
        {
            ConvertToFacebookCommand = new ConvertToFacebookCommand();
            _messanger.Register(OnSentMessage, "DirFilesListBox_SelectedDocumentChanged");
        }

        public void OnSentMessage(object source, MessageEventArgs e)
        {
            _selectedDocument = e.MessageObject;
        }
    }
    public class ConvertToFacebookCommand : SingleFuncitonBaseCommand
    {
        public override void ButtonFunction(object parameter)
        {
            ConvertToFacebookFn.Convert(parameter);
        }

        public override void NotExacutable(object parameter)
        {
            ButtonNotExacutable.Report(parameter);
        }
    }
}

ViewModelBase

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace CatalogInterface
{
    public abstract class SingleFuncitonBaseCommand : ICommand
    {
        private bool _canExecute { get; set; } = true;
        public SingleFuncitonBaseCommand()
        {

        }
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public virtual bool CanExecute(object parameter)
        {
            return true;
        }

        public virtual void Execute(object parameter)
        {
            if (_canExecute && parameter != null)
            {
                try
                {
                    _canExecute = false;
                    ButtonFunction(parameter);
                }
                finally
                {
                    _canExecute = true;
                }
                return;
            }
            NotExacutable(parameter);
        }
        public abstract void ButtonFunction(object parameter);
        public abstract void NotExacutable(object parameter);
    }
}

我的SelectedDocument属性由OnSentMessage事件设置,该事件在用户单击列表框中的文件时更新属性。我相信该对象是一个FileInfo对象。我可以确认事件正在正常启动并且正在设置属性。

此外,如果我将SelectedDocument属性更改为只读属性并将其设置为FileInfo对象,则会将其传递给参数。

我的OnSentMessage事件和CommandParameter绑定是否可以在两个不同的对象上运行?

如何进一步调试?

全部谢谢。

0 个答案:

没有答案
相关问题