WPFXaml页面中绑定失效问题

发布时间 2023-11-13 10:01:21作者: 音乐随生活
在xaml中绑定viewModel中的控件,特别是我们自己写的自定义控件,容易出现问题,绑定不成功,在这里我建议给相应的控件加上名字直接用c#代码进行绑定。
 public partial class Translation : UserControl
 {
     readonly TranslationViewModel viewModel;
     public Translation()
     {

         InitializeComponent();
         this.DataContext = viewModel = new TranslationViewModel();
         page.SetBinding(PagingControl.TotalCountProperty, new Binding("TotalCount") { Source = viewModel, Mode = BindingMode.OneWay });
         page.SetBinding(PagingControl.CommandProperty, new Binding("PagingQueryCommand") { Source = viewModel });
     }

这里举了两个例子 ,一个是属性的绑定,一个是命令的绑定,关键是给相关ViewModel一个实例名,然后将其设置为binding的source