简体   繁体   中英

Binding viewmodel property to view property in code behind using Prism

I use C# 7, Prims 6.3 and SyncFusion Grid Control for developing my application ( https://help.syncfusion.com/wpf/grid/getting-started ).
My problem is next: Grid Control is just "emulate" WPF-style, so I can add it into XAML: <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Grid.Row="0" CanContentScroll="True"> <syncfusion:GridControl x:Name="WorksheetGrid"/> </ScrollViewer>

But I can't bind it with my viewmodel properties which contain all data for table.
So I used EventAggregator for sending new data to view from viewmodel:

`

private void OnSelectWorksheetEvent(WorksheetSelectedEventArgs args) {
  _regionManager.RequestNavigate(RegionNames.RootRegion, "WorksheetView");
  _eventAggregator.GetEvent<WorksheetDataRecievedEvent>().
      Publish(new WorksheetDataEventArgs(_model.ReadRowHeader(args.WshCode),
      _model.ReadColHeader(args.WshCode), _model.ReadCells(args.WshCode)));
}

`

But this is ugly arhictecture, because I duplicate all data structure in view code behind:

`

void OnWorksheetDataRecievedEvent(WorksheetDataEventArgs args) {
            WorksheetGrid.QueryCellInfo -= new GridQueryCellInfoEventHandler(gridControl_QueryCellInfo);

            ClearWorksheetData();
            ClearWorksheetModel();

            RowHeaderHelper.RowHeader = args.RowHeader;
            ColHeaderHelper.ColHeader = args.ColHeader;
            CellHelper.Cells = args.Cells;

            WorksheetGrid.Model.RowCount = RowHeaderHelper.HeaderSize() + 1;
            WorksheetGrid.Model.ColumnCount = ColHeaderHelper.HeaderSize() + 1;

            WorksheetGrid.QueryCellInfo += new GridQueryCellInfoEventHandler(gridControl_QueryCellInfo);
            WorksheetGrid.Model.ResizeRowsToFit(GridRangeInfo.Row(0), GridResizeToFitOptions.IncludeHeaders);
        }

`

I understand that direct access to viewmodel properties from view is anti-mvvm, but I'm limited with SyncFusion gui component

So my questions: 1. How can I access to viewmodel's properties from view code behind? 2. How can I trigger some function in view code behind when property in viewmodel is changed?

Thanks.

It's been a while since I used Prism (and yes, your current solution is very anti-mvvm and I would suggest you try and wrap the problematic controls), but I think that for:

  1. In the view this.DataContext should be the view model.

  2. I would register to the PropertyChanged event in the view model to get updates on it's values (view models usually implement the INotifyPropertyChanged interface).

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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