使用MVVMlight 绑定自定义控件(userControl)

MVVMLight 之绑定自定义控件(userControl)

  1.在做WPF程序的时候,避免不了会用到自定义控件;在一个VIEW中使用userControl,在userControl中很可能还包含有userControl; 

  2.viewModel 中定义好数据;

  3.自定义控件如下方式绑定

    <ContentControl>
        <HardwareControl:DeviceManageListView DataContext="{Binding DataContext,RelativeSource={RelativeSource AncestorType=Window}}"/>
    </ContentControl>

  这个时候自定义控件中的DataContext与当前viewModel是一致的;根据需要绑定即可;

  4.在USerControl 中包含UserControl,绑定方式如下:

    

    <ContentControl Grid.Column="2" >
      <my:DeviceManageView DataContext="{Binding DataContext,RelativeSource={RelativeSource AncestorType=UserControl}}" />
    </ContentControl>

  这个时候自定义控件中的DataContext与当前UserControl是一致的;根据需要绑定即可;

  

posted @ 2021-05-20 18:11  cdxy2005  阅读(430)  评论(0编辑  收藏  举报