简体   繁体   English

如何在 WPF 中不绑定的情况下将网格属性传递给 ViewModel

[英]How to pass grid property to ViewModel without Binding in WPF

I'm using DevExpress XAML Framework.我正在使用 DevExpress XAML 框架。 In my grid I have CheckBoxSelectorColumn, which is delivered with the Framework (the easiest way to make multiple selection I've found).在我的网格中,我有 CheckBoxSelectorColumn,它随框架一起提供(我发现进行多项选择的最简单方法)。

I cannot use binding to get SelectedItems, instead in my main.xaml.cs I use [gridName].SelectedItems.我不能使用绑定来获取 SelectedItems,而是在我的 main.xaml.cs 中我使用 [gridName].SelectedItems。

Is there a way to pass SelectedItems to the MainViewModel?有没有办法将 SelectedItems 传递给 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我的用户控件.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.我使用ShowCheckBoxSelectorColumn属性来获取每行中有一个复选框的列,以便进行多项选择。 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我的用户控件.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.使用grid.SelectedItems我可以在视图的 checkboxColumn 中选择这些项目。 I wanted to pass SelectedItems to the ViewModel in order reach them from the Button in my RibbonComponent.我想将 SelectedItems 传递给 ViewModel,以便从 RibbonComponent 中的 Button 到达它们。 That was my question.那是我的问题。

[SOLUTION] [解决方案]

I used DevExpress.Mvvm.Messenger to call EntriesHandle_Button() from the RibbonControl:我使用 DevExpress.Mvvm.Messenger 从 RibbonControl 调用EntriesHandle_Button()

ribbon.xaml色带.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功能区视图模型.cs

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

RibbonCommands.cs功能区命令.cs

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

MyUserControl.xaml.cs我的用户控件.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"); }      
        }
}


GridControl fully supports SelectedItems binding when CheckBoxSelectorColumn is used.使用CheckBoxSelectorColumn时,GridControl 完全支持SelectedItems绑定。 One important thing is that the project bound to SelectedItems should be initialized at the view model level.一件重要的事情是绑定到 SelectedItems 的项目应该在视图模型级别初始化。 Meaning that if you bound SelectedItems to a property SelectedCustomers , it's necessary to initialize SelectedCustomers with an empty collection.这意味着如果您将SelectedItems绑定到属性SelectedCustomers ,则有必要使用空集合初始化SelectedCustomers For example ObservableCollection< Customer>例如 ObservableCollection<Customer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM