C# 数据绑定的几种方式

        <!--第一种默认的方式 指定绑定的数据源和路径-->
        <TextBlock Text="{Binding Source={StaticResource ViewModel}, Path=temp}" ></TextBlock>
        <!--第二种采用Element方式-->
            <TextBlock Text="{Binding ElementName=mainwindow, Path=Title}"></TextBlock>
        <!--第三种采用相对源RelativeSource方式-->
        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=Window,Mode=FindAncestor},Path=Title }" Foreground="Orange"></TextBlock>
        <!--第4种采用父对象Datacontext进行数据源指定-->
        <TextBlock Text="{Binding Path=value}" DataContext="{StaticResource  ViewModel}"></TextBlock>

posted @ 2021-10-28 14:34  岳振威  阅读(1134)  评论(0编辑  收藏  举报