-1

I'm using DevExpress XAML Framework. In my grid I have CheckBoxSelectorColumn, which is delivered with the Framework (the easiest way to make multiple selection I've found).

I cannot use binding to get SelectedItems, instead in my main.xaml.cs I use [gridName].SelectedItems.

Is there a way to pass SelectedItems to the MainViewModel?

[EDITED]

I'm sorry for that my question was unclear and without code examples. I'm going to provide some parts of my code.

MyUserControl.xaml

<dxg:GridControl Name="grid" SelectionMode="MultipleRow" AutoGenerateColumns="None" EnableSmartColumnsGeneration="false" ItemsSource="{Binding BugList}" Margin="0,0,0,20" Grid.RowSpan="2">
            <dxg:GridControl.View>
                <dxg:TableView
                               AllowEditing="False" ShowCheckBoxSelectorColumn="True"
                               HorizontalScrollbarVisibility="Auto"
                               />
            </dxg:GridControl.View>
            <dxg:GridColumn FieldName="Description" Header="Description"/>

I used ShowCheckBoxSelectorColumn property to get a column with a checkbox in each row for multiple select. Disadvantage of the property, and a benefit at the same time, is that selected items cannot be passed through the binding (if my research was correct), but they can be easily passed to the view backend in the following way:

MyUserControl.xaml.cs

 public void EntriesHandle_Button()
        {
            if (grid.SelectedItems.Count != 0)
            {
                if (DXMessageBox.Show("Do you really want to remove selected Items from your List?", "Question",   
                            MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    //remove selected entries
                }
            }
            else { MessageBox.Show("Please select entries", "Warning"); }      
        }

With grid.SelectedItems I can get those items were selected in the checkboxColumn in the view. I wanted to pass SelectedItems to the ViewModel in order reach them from the Button in my RibbonComponent. That was my question.

[SOLUTION]

I used DevExpress.Mvvm.Messenger to call EntriesHandle_Button() from the RibbonControl:

ribbon.xaml

<dxb:BarButtonItem Name="BRemove" Content="Remove selected">
                            <dxmvvm:Interaction.Behaviors>
                                <dxmvvm:EventToCommand EventName="ItemClick" Command="{Binding RemoveEntries_OnClick}"/>
                            </dxmvvm:Interaction.Behaviors>
                        </dxb:BarButtonItem>

RibbonViewModel.cs

public class RibbonViewModel : ViewModelBase
    {          
        private readonly IRibbonCommands _ribbonCommands;    
        public RibbonViewModel(IRibbonCommands ribbonCommands)
        {
            _ribbonCommands = ribbonCommands;
            RemoveEntries_OnClick = new DelegateCommand(_ribbonCommands.RemoveEntries);
        }

RibbonCommands.cs

public class RibbonCommands : IRibbonCommands
    {
        public void RemoveEntries()
        {
            string message = "remove";
            Messenger.Default.Send(message);
        }
    }

MyUserControl.xaml.cs

public partial class MyUserControl: UserControl
    {
        public MyUserControl()
        {
            InitializeComponent();
            DataContext = new MyUserControlViewModel();
            Messenger.Default.Register<string>(this, EntriesHandle_Button);
        }
       public void EntriesHandle_Button(string message)
        {
            if (grid.SelectedItems.Count != 0)
            {
              switch (message)
                {
               case "remove":
                if (DXMessageBox.Show("Do you really want to remove selected Items from your List?", "Question",   
                            MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    //remove selected entries
                }
             break;
              }
            }
            else { MessageBox.Show("Please select entries", "Warning"); }      
        }
}


Community
  • 1
  • 1
maks-ua
  • 1
  • 2
  • something like this I would assume, I haven't uesd DevExpress. https://stackoverflow.com/a/22392637/3225 – kenny Mar 15 '19 at 14:49
  • Is the DataContext of Main.xaml set to your MainViewModel? Perhaps you can do `(DataContext as MainViewModel).YourSelectedItemsProperty = [gridName].SelectedItems` in your Main.xaml.cs. However a proper MVVM solution would be better ;) – JTK Mar 15 '19 at 14:53
  • 1
    If it's a proper MVVM, then you could have view models for each item with property `IsSelected`. That property can be set by the view or maybe even bound directly to some other property of control (I don't use DevExpress), like it's typically done for `ListViewItem` ([example](https://stackoverflow.com/a/15173085/1997232)). – Sinatr Mar 15 '19 at 14:54
  • Please consider providing a [mcve]. Also don't forget to read [ask] a good question. – dymanoid Mar 15 '19 at 15:05
  • Binding SelectedItems to your ViewModel is possible, you need to use an ObservableCollection property. – Roger Leblanc Mar 17 '19 at 00:06

1 Answers1

0

GridControl fully supports SelectedItems binding when CheckBoxSelectorColumn is used. One important thing is that the project bound to SelectedItems should be initialized at the view model level. Meaning that if you bound SelectedItems to a property SelectedCustomers, it's necessary to initialize SelectedCustomers with an empty collection. For example ObservableCollection< Customer>

Alex Russkov
  • 323
  • 2
  • 8