我很难将简单的静态字符串属性绑定(bind)到文本框。
这是具有静态属性的类:
public class VersionManager
{
private static string filterString;
public static string FilterString
{
get { return filterString; }
set { filterString = value; }
}
}
在我的 xaml 中,我只想将此静态属性绑定(bind)到 TextBox:
<TextBox>
<TextBox.Text>
<Binding Source="{x:Static local:VersionManager.FilterString}"/>
</TextBox.Text>
</TextBox>
一切都会编译,但在运行时,我收到以下异常:
Cannot convert the value in attribute 'Source' to object of type 'System.Windows.Markup.StaticExtension'. Error at object 'System.Windows.Data.Binding' in markup file 'BurnDisk;component/selectversionpagefunction.xaml' Line 57 Position 29.
知道我做错了什么吗?
最佳答案
如果绑定(bind)需要双向,则必须提供路径。
如果类不是静态的,有一个技巧可以对静态属性进行双向绑定(bind):在资源中声明该类的虚拟实例,并将其用作绑定(bind)的源。
<Window.Resources>
<local:VersionManager x:Key="versionManager"/>
</Window.Resources>
...
<TextBox Text="{Binding Source={StaticResource versionManager}, Path=FilterString}"/>
关于wpf - 绑定(bind)到静态属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/936304/