按钮不会与使用caliburn micro的viewmodel中的函数绑定

时间:2014-07-13 12:29:26

标签: c# wpf mvvm caliburn

我在视图中有以下xaml代码:

<UserControl x:Class="Klanten_beheer.Views.CustomerListView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit">

    <DockPanel LastChildFill="True" Height="Auto" Width="Auto" Grid.Row="0">
        <Button x:Name="ToLastRecord" Content="&lt;&lt;" Background="LightSkyBlue"/>

以下代码在我的viewmodel中:

public class CustomerListViewModel : PropertyChangedBase
{
    #region Fields

    private IObservableCollection<Client> m_clients;

    #endregion

    #region Properties

    public int Position { get; private set; }

    public IObservableCollection<Client> Clients
    {
        get { return Clients; }
        set { Clients = value; }
    }

    public Client SelectedClient
    {
        get 
        {
            if ((Position < m_clients.Count) && (Position > -1))
            {
                return m_clients[Position];
            }
            else
            {
               return m_clients[0];
            }
        }
        set 
        {
            Position = m_clients.IndexOf(m_clients.Where(x => x.ClientNumber ==        value.ClientNumber).FirstOrDefault());
        }
    }

    #endregion

    #region Constructors

    CustomerListViewModel(int position)
    {
        using (ClientDbEntities ctx = new ClientDbEntities())
        {
            var m_clients = (from client in ctx.Clients
                            orderby client.ClientNumber
                            select client).ToList();

            if (m_clients.Count != 0 && position < m_clients.Count)
            {
                Position = position;
            }
            else if (position >= m_clients.Count)
            {
                // Do nothing
            }
            else
            {
                Position = -1;
            }
        }
    }

        public CustomerListViewModel()
        {
            Position = 0;
        }

        #endregion

        #region Public functions

        public bool CanToLastRecord()
        {
            return true;
        }

        public void ToLastRecord()
        {
            System.Windows.MessageBox.Show("ToLastRecord");
        }

        #endregion
    }
}

我希望按下按钮时触发函数ToLastRecord。 由于某种原因,该功能永远不会是触发器。

1 个答案:

答案 0 :(得分:0)

我知道它已经有一段时间了 - 但永远都会遇到这种情况, 你真的应该看看Caliburn.micro cheat-sheet

答案就在页面顶部 - 为了将操作附加到默认事件(按钮点击确实计数),您需要使用

<Button x:Name="ToLastRecord" Content="&lt;&lt;" Background="LightSkyBlue"  cal:Message.Attach="ToLastRecord"/>

不要忘记将以下行添加到UserControl元素:

xmlns:cal="http://www.caliburnproject.org"