Search code examples
wpfmvvmcaliburn

Caliburn Micro Listbox move item up or down


I've built an UI following this example... and would like to add two buttons to re-order listbox selected item (Move up - Move Down) in the "There" list. Any ideas as to do this using Caliburn Micro?


Solution

  • You need to add a property for SelectedThereItem and bind it in ThereList:

    XAML:

    <ListBox x:Name="ThereList" SelectedItem="{Binding ThereSelectedItem}" ... />
    <!-- Add buttons for ThereMoveUp and ThereMoveDown - use Caliburn naming convention -->
    <Button x:Name="ThereMoveUp"/>
    <Button x:Name="ThereMoveDown"/>
    

    ViewModel:

    private Person _thereSelectedItem;
    public Person ThereSelectedItem
    {
        get { return _thereSelectedItem; }
        set
        {
            _thereSelectedItem = value;
            NotifyOfPropertyChange(() => ThereSelectedItem);
            NotifyOfPropertyChange(() => CanThereMoveDown);
            NotifyOfPropertyChange(() => CanThereMoveUp); 
        }
    }
    
    // Add event method handlers for ThereMoveUp/Down
    public bool CanThereMoveUp { get { return _thereSelectedItem != null; } }
    public void ThereMoveUp
    {
        // Logic to move up
    }
    
    public bool CanThereMoveDown { get { return _thereSelectedItem != null; } }
    public void ThereMoveDown
    {
        // Logic to move down
    }