Developer1
Developer1

Reputation: 1

Button doesn't bind to function in viewmodel with caliburn micro

I have the following xaml code in my view:

<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"/>

And the following code in my 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
    }
}

I would expect that the function ToLastRecord was trigger when the button is pushed. For some reason the function is never triggert.

Upvotes: 0

Views: 1918

Answers (1)

Koby Yehezkel
Koby Yehezkel

Reputation: 218

I know it's been awhile - but forever who'll come up to this, you should really take a look at Caliburn.micro cheat-sheet

The answer is right there at the top of the page - in order to attach an action to a default event (on buttons clicks do count) you need to use

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

Don't forget to add the following line to UserControl element:

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

Upvotes: 1

Related Questions