加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用MVVM的WPF:使用RelativeSource的DataBinding

发布时间:2020-12-15 04:31:57 所属栏目:百科 来源:网络整理
导读:我有一个控件,在该控件内,我有一个带有数据tempalte的资源: DataTemplate DataType="{x:Type local:FlowModel}" Image Source="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type vm:MainViewModel}},Path=MainViewModel.ImagePa
我有一个控件,在该控件内,我有一个带有数据tempalte的资源:
<DataTemplate DataType="{x:Type local:FlowModel}">
    <Image Source="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type vm:MainViewModel}},Path=MainViewModel.ImagePath}"/>
  </DataTemplate>

 xmlns:vm="clr-namespace:CortexMonitoringTool.ViewModel"

我将vm设置为我的ViewModel文件夹,我正在实现mvvm.我不能让我的约束力工作,我不确定为什么不.

有人可以告诉我,如果我的相对绑定是正确的,它是否可以在我的MainViewModel类中实际看到我的属性’ImagePath’?

public String ImagePath
    {
        get
        {
            return _imagePath;
        }
        set
        {
            if (_imagePath == value)
            {
                return;
            }
            _imagePath = value;
            RaisePropertyChanged("ImagePath");
        }
    }

谢谢.

解决方法

您查看模型不是Visual树的一部分.所以找到祖先类型不会在那里工作.如果您找到具有datacontext的根父级,则可以使用其属性与like绑定.
<Image Source={...... Path=DataContext.MyProperty}"/>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读