0

我被这个难住了。尽管绑定到具有值的属性,为什么命令参数会持续为空。

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);
        }
    }
}

视图模型库

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 绑定是否可以在两个不同的对象上运行?

我该如何进一步调试呢?

谢谢大家。

4

0 回答 0