Can I bind a WPF control to a field's property?

前端 未结 3 1102
闹比i
闹比i 2020-12-11 18:43

Because I needed to split some functionality between classes, I\'ve arrived at the following situation

xaml code



        
3条回答
  •  天涯浪人
    2020-12-11 19:26

    Instead of binding an element to a field's property I changed the DataContext of the element to the required field.

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            MainWindowView mainWindowView = new MainWindowView();
            var mainWindowViewModel = new MainWindowViewModel();
            mainWindowView.DataContext = mainWindowViewModel;
            mainWindowView.pagerView.DataContext = mainWindowViewModel.pager;
            mainWindowView.Show();
        }
    

    In this example I have a DataGrid and Pager (first, prev, next, last page) below it. The elements of the MainWindowView (including the DataGrid) are binded to properties in the MainWindowViewModel but the pager buttons are binded to the properties of mainWindowViewModel.pager.

    MainWindowView:

        
        
    

    PagerView:

    
    
        

提交回复
热议问题