gpt4 book ai didi

WPF - 有没有办法以编程方式评估绑定(bind)?

转载 作者:行者123 更新时间:2023-12-03 17:21:42 25 4
gpt4 key购买 nike

有谁知道如何获取与绑定(bind)关联的当前值?我最近遇到了一个问题,我想在 WPFToolKit DataGrid 中获取与特定单元格关联的值 - 所以我创建了一个函数来获取路径字符串,在“。”上拆分。并尝试在循环中使用 PropertyDescriptor,尝试获取绑定(bind)值。当然有更好的方法:)。如果有人能指出我正确的方向,我会永远爱你。

谢谢,

查尔斯

最佳答案

由于给出的答案链接现在只能在 webarchive 上找到,我复制了那里给出的答案:

public static class DataBinder
{
private static readonly DependencyProperty DummyProperty = DependencyProperty.RegisterAttached(
"Dummy",
typeof(Object),
typeof(DependencyObject),
new UIPropertyMetadata(null));

public static object Eval(object container, string expression)
{
var binding = new Binding(expression) { Source = container };
return binding.Eval();
}

public static object Eval(this Binding binding, DependencyObject dependencyObject = null)
{
dependencyObject = dependencyObject ?? new DependencyObject();
BindingOperations.SetBinding(dependencyObject, DummyProperty, binding);
return dependencyObject.GetValue(DummyProperty);
}
}

例子:
public partial class PropertyPathParserDemo : Window
{
public PropertyPathParserDemo()
{
InitializeComponent();
Foo foo = new Foo() { Bar = new Bar() { Value = "Value" } };
this.Content = DataBinder.Eval(foo, "Bar.Value");
}

public class Foo
{
public Bar Bar
{
get;
set;
}
}

public class Bar
{
public string Value
{
get;
set;
}
}
}

关于WPF - 有没有办法以编程方式评估绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/703741/

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