gpt4 book ai didi

wpf - 绑定(bind)一个绑定(bind)的路径属性

转载 作者:行者123 更新时间:2023-12-04 19:28:22 25 4
gpt4 key购买 nike

是否可以将绑定(bind)的 Path 属性绑定(bind)到另一个属性?

我想实现这段代码:

Text="{Binding Path={Binding Path=CurrentPath}}"

所以我可以动态调整我的实际绑定(bind)所引用的属性。

感谢您的帮助
强尼

最佳答案

我自己解决了。

这是解决方案,我希望它可以帮助任何像我一样遇到同样问题的人。

public class CustomBindingBehavior : Behavior<FrameworkElement>
{
public bool IsBinding
{
get
{
return (bool)GetValue(IsBindingProperty);

}
set
{
SetValue(IsBindingProperty, value);
}
}

public string PropertyPath
{
get
{
return (string)GetValue(PropertyPathProperty);

}
set
{
SetValue(PropertyPathProperty, value);
}
}

public static DependencyProperty
PropertyPathProperty = DependencyProperty.Register("PropertyPath", typeof(string),
typeof(CustomBindingBehavior), null);

public static DependencyProperty
IsBindingProperty = DependencyProperty.Register("IsBinding", typeof(bool),
typeof(CustomBindingBehavior), null);

protected override void OnAttached()
{
if (AssociatedObject is TextBlock)
{
var tb = AssociatedObject as TextBlock;
tb.Loaded += new RoutedEventHandler(tb_Loaded);
}
}

private void tb_Loaded(object sender, RoutedEventArgs e)
{
AddBinding(sender as TextBlock, TextBlock.TextProperty);
}

private void AddBinding(DependencyObject targetObj, DependencyProperty targetProp)
{
if (IsBinding)
{
Binding binding = new Binding();
binding.Path = new PropertyPath(this.PropertyPath, null);

BindingOperations.SetBinding(targetObj, targetProp, binding);
}
else
{
targetObj.SetValue(targetProp, this.PropertyPath);
}
}
}

下面是 XAML 中的实现:
<TextBlock >
<i:Interaction.Behaviors>
<behaviors:CustomBindingBehavior PropertyPath="{Binding Path=HeaderPropertyBinding}" IsBinding="{Binding Path=HeaderIsBinding}" />
</i:Interaction.Behaviors>
</TextBlock>

问候
强尼

关于wpf - 绑定(bind)一个绑定(bind)的路径属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13524588/

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