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

2021/5/20 18:58:16

本文主要是介绍使用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是一致的;根据需要绑定即可;

  



这篇关于使用MVVMlight 绑定自定义控件(userControl)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程