gpt4 book ai didi

.net - 如何将 Binding.Path 属性绑定(bind)到基础数据?

转载 作者:行者123 更新时间:2023-12-04 11:17:54 25 4
gpt4 key购买 nike

我正在尝试以非常动态的方式绑定(bind) TextBlock 的 Text 属性。我需要从底层对象获取路径。

这是数据模板:

<DataTemplate DataType={x:Type local:DummyClass}>
<TextBlock Text={Binding Path=???} />
</DataTemplate>

DummyClass 对象有一个名为“FieldValuePath”的属性 - 需要放在 ???是。

这背后的想法是数据模板应该是一个用于查看/编辑任何对象的任何属性的 GUI。因此,最好能够声明 XAML,它将一些控件(文本框、文本 block 、日期选择器等)绑定(bind)到给定属性。

也许有人对如何实现这样的事情有任何建议?

最佳答案

如果您在后面的代码中创建绑定(bind),那么您可以让它工作。例如,一个简单的代码生成绑定(bind)是:

Binding binding = new Binding("BindingPath");
binding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(textBoxName, TextBox.TextProperty, binding);

由于此绑定(bind)中的路径(“BindingPath”)只是一个字符串,因此该字符串可以来自任何可用对象。

不过,您需要 Hook 数据项的创建以设置这些绑定(bind)。

根据您的评论的另一种可能性:

This blog post概述了一种通过从 MarkupExtension 继承来创建自定义绑定(bind)类的方法。您可以以此为起点,将我的建议包装成可重复使用的 xaml 标记,以用于您的特殊绑定(bind)案例。

更多想法:

好的,这是一个有趣的问题,所以我决定花一点时间看看我是否能想出一个可行的解决方案。对于以下代码示例的长度,我提前道歉...

基于我在上面链接到的博客文章的解决方案,我创建了这个类:
public class IndirectBinder : MarkupExtension
{
public string IndirectProperty { get; set; }

public override object ProvideValue(IServiceProvider serviceProvider)
{
//try to get bound items for our custom work
DependencyObject targetObject;
DependencyProperty targetProperty;
bool status = TryGetTargetItems(serviceProvider, out targetObject, out targetProperty);

if (status)
{
Control targetControl = targetObject as Control;
if (targetControl == null) return null;

//Find the object to take the binding from
object dataContext = targetControl.DataContext;
if (dataContext == null) return null;

//Reflect out the indirect property and get the value
PropertyInfo pi = dataContext.GetType().GetProperty(IndirectProperty);
if (pi == null) return null;

string realProperty = pi.GetValue(dataContext, null) as string;
if (realProperty == null) return null;

//Create the binding against the inner property
Binding binding = new Binding(realProperty);
binding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(targetObject, targetProperty, binding);

//Return the initial value of the binding
PropertyInfo realPi = dataContext.GetType().GetProperty(realProperty);
if (realPi == null) return null;

return realPi.GetValue(dataContext, null);

}

return null;

}

protected virtual bool TryGetTargetItems(IServiceProvider provider, out DependencyObject target, out DependencyProperty dp)
{
target = null;
dp = null;
if (provider == null) return false;

//create a binding and assign it to the target
IProvideValueTarget service = (IProvideValueTarget)provider.GetService(typeof(IProvideValueTarget));
if (service == null) return false;

//we need dependency objects / properties
target = service.TargetObject as DependencyObject;
dp = service.TargetProperty as DependencyProperty;
return target != null && dp != null;
}

您可以将此新标记与以下 xaml 一起使用:
<TextBox Text="{local:IndirectBinder IndirectProperty=FieldValuePath}"/>

其中 TextBox 可以是任何从控件继承的类,而 Text 可以是任何依赖属性。

显然,如果您需要公开任何其他数据绑定(bind)选项(例如单向或双向绑定(bind)),那么您需要向该类添加更多属性。

虽然这是一个复杂的解决方案,但它比使用转换器的一个优点是最终创建的绑定(bind)是针对实际的内部属性而不是对象。这意味着它正确地响应 PropertyChanged 事件。

关于.net - 如何将 Binding.Path 属性绑定(bind)到基础数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/777991/

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