我想在我的代码中从Binding获取值并将其用作字符串。我怎么能这样做?
Binding b = new Binding("MyProperty")
{
Source = myobject
};
//[...]
string value = b //HOW TO GET VALUE FROM b ?
顺便说一句: 我希望在检索此值时调用附加到Binding的Converter。
答案 0 :(得分:0)
我发现解决方案可能是DependencyProperty的辅助类。
辅助课程
public class TestClass : FrameworkElement
{
public string MyProperty
{
get { return (string)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(TestClass), new PropertyMetadata(""));
}
将转化绑定到字符串
Binding b = new Binding("MyProperty") { Source = myobject };
TestClass tc = new TestClass { DataContext = b };
BindingOperations.SetBinding(tc, TestClass.MyPropertyProperty, b);
string txt = tc.MyProperty;
<强>优点:强>
<强>缺点:强>