gpt4 book ai didi

c# - 绑定(bind)到 UserControl 中的属性

转载 作者:太空宇宙 更新时间:2023-11-03 22:14:55 24 4
gpt4 key购买 nike

我有一个用户控件,我想在其中公开一个名为 ExpressionText 的属性,并在可以为此属性定义一个 xaml 绑定(bind)。所以我创建了一个依赖属性

public static readonly DependencyProperty EditorText =DependencyProperty.Register("EditorText", typeof(string), typeof(MyUerControl));

public string ExpressionText 
{
get
{
return (string)GetValue(EditorText);
}
set
{
SetValue(EditorText, value);
}
}

我在 xaml 中这样做。

     <controls:MyUerControl x:Name="textEditor" ExpressionText="{Binding 
Path=Expression,Mode=TwoWay}" />

但是我明白了

无法在 MyUserControl 类型的 ExpressionText 属性上设置绑定(bind)。可以设置绑定(bind)仅在 Dependency 对象类型错误的 depenedecy 属性上。

我的方法有问题吗?我该如何解决这个问题?

最佳答案

这应该有效:

public static DependencyProperty EditorTextProperty = DependencyProperty.Register("ExpressionText", typeof(string), typeof(MyUserControl),
new PropertyMetadata(new PropertyChangedCallback((s, e) =>
{ })));
public string ExpressionText
{
get
{
return (string)base.GetValue(EditorTextProperty);
}
set
{
base.SetValue(EditorTextProperty, value);
}
}

关于c# - 绑定(bind)到 UserControl 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5432130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com