简体   繁体   English

将自定义属性绑定到上下文

[英]Bind Custom Property to Context

I like to bind a custom property (owner Window) to my datacontext. 我喜欢将自定义属性(所有者窗口)绑定到我的datacontext。 How to do these in xaml. 如何在xaml中执行这些操作。 I can not access on these property because my class is window and not MyView, its Window 我无法访问这些属性,因为我的课程是窗口,而不是MyView,即它的Window
<Window x:Class="MyNamespace.MyView"
By changing my xaml to MyView class i get some errors that my class needs a inheritance from window. 通过将xaml更改为MyView类,我得到一些错误,即我的类需要从window继承。

Codebehind: 代码背后:

DependencyProperty MyValueProperty, Property MyValue

Xaml: Bind MyValue to my datacontext. Xaml:将MyValue绑定到我的数据上下文。

I want these 我要这些 在此处输入图片说明

If i try to change the class name inside xaml: 如果我尝试更改xaml中的类名:
在此处输入图片说明

I get these error 我得到这些错误
在此处输入图片说明

您可以实例化<local:MyView xmlns:local="YourNameSpaceToMyView"这样的控件,从而可以使用DP

You can set your DataContext to the current instance of your window class like this: 您可以将DataContext设置为窗口类的当前实例,如下所示:

<Window x:Class="MyNamespace.MyView" 
        DataContext="{Binding RelativeSource={RelativeSource self}}">

Then you can access the underlying properties easily. 然后,您可以轻松访问基础属性。

To bind to your custom property, you can do something like this: 要绑定到您的自定义属性,您可以执行以下操作:

DataContext="{Binding RelativeSource={RelativeSource self}, Path=MyProperty}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM