C# 将实例化属性绑定到UI

C# 将实例化属性绑定到UI,c#,.net,wpf,C#,.net,Wpf,我有这门课: public class property : DependencyObject, INotifyPropertyChanged { private string _myproperty; public string MyProperty { get { return this._myproperty; } set { this.

我有这门课:

public class property : DependencyObject, INotifyPropertyChanged
{
    private string _myproperty;

    public string MyProperty
    {
        get
        {
            return this._myproperty;
        }
        set
        {
            this._myproperty = value;
            NotifyPropertyChanged("MyProperty");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string sproperty)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(sproperty));
        }
    }
}
在主窗口中,我创建了这个类的一个实例
myclass xx=newmyclass(),其中我用字符串数据填充属性并将其绑定到XAML,如下所示:

<Window.Resources>
    <local:property x:Key="prop"></local:property>
</Window.Resources>
除非我使用现有资源,否则这将不起作用:

var property = (local:property)Resources["prop"];
是否有其他方法来更新
文本框
,而不是使用资源?我想使用普通的类实例化。

如果你说
Text=“{Binding Path=MyProperty,Source={StaticResource prop}}”BorderBrush=“#FFC7CACC”/>
意味着您的VM是属性类的实例

尝试用网格包围文本框,并用poperty clas的实例设置网格dataContext

我是说

<Grid DataContext="from view or from behind assign your vm= new property()">
 <TextBox Text="{Binding Path=MyProperty" ....../>
</Grid>

试试这个:

<Window.DataContext>
 <local:property/>
<Window.DataContext>

<TextBox Text="{Binding MyProperty}"/>

设置数据上下文后,只需尝试构建应用程序,如果能够在本地命名空间中找到属性类,则构建将成功

构建应用程序后,如果成功,您可以尝试设置绑定,并且Intellisense将在绑定选项中自动显示MyProperty

如果这不起作用,请尝试使用“属性”面板设置数据上下文和绑定。也许在视觉上你能把事情做好


试试看,如果失败了,告诉我哪里出了问题。

local:local:property?这里只有一个loccal,对不起,local:property…为什么要将此属性类用作资源?只需将MyProperty放入UI的viewmodel中,并从中绑定即可。您已经尝试过此方法,但没有luckSo解决方案!!这让我好奇:我已经尝试过了,但不幸的是,这不起作用
<Window.DataContext>
 <local:property/>
<Window.DataContext>

<TextBox Text="{Binding MyProperty}"/>