在WPF中,可以通过编程方式评估绑定。以下是一些常用的方法:
BindingOperations.GetBindingExpression
方法获取绑定表达式,然后调用UpdateTarget
或UpdateSource
方法更新目标或源。BindingExpression bindingExpression = BindingOperations.GetBindingExpression(myControl, MyControl.MyPropertyProperty);
bindingExpression.UpdateTarget();
DependencyPropertyDescriptor
类获取依赖项属性的描述符,然后调用GetValue
或SetValue
方法获取或设置属性值。DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(MyControl.MyPropertyProperty, typeof(MyControl));
object value = descriptor.GetValue(myControl);
descriptor.SetValue(myControl, newValue);
Binding
类的ProvideValue
方法获取绑定的值。Binding binding = new Binding("MyProperty");
object value = binding.ProvideValue(new BindingProvideValueTarget(myControl, null, null, null, null, null, null));
MultiBinding
类的ProvideValue
方法获取多绑定的值。MultiBinding multiBinding = new MultiBinding();
multiBinding.Bindings.Add(new Binding("MyProperty1"));
multiBinding.Bindings.Add(new Binding("MyProperty2"));
multiBinding.Converter = new MyMultiBindingConverter();
object value = multiBinding.ProvideValue(new BindingProvideValueTarget(myControl, null, null, null, null, null, null));
这些方法可以帮助您在WPF中以编程方式评估绑定。
领取专属 10元无门槛券
手把手带您无忧上云