gpt4 book ai didi

c# - 绑定(bind)到后面代码中的相关源

转载 作者:太空狗 更新时间:2023-10-29 20:46:00 26 4
gpt4 key购买 nike

在我的 UserControl 中,我的 XAML 中有以下代码

<TextBlock Grid.Row="2" Text="{Binding Path=StartTime,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorLevel=1, AncestorType=Window}}" />

这只是从父窗口获取属性的值,而且效果很好。

我如何在后面的代码中执行此操作?

Binding b = new Binding();
b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor,
typeof(Window), 1);
b.Path = "StartTime";

myProperty = b.value;// obviously there is no b.value but this
// is what I'm trying to achieve.

//BindingOperations.SetBinding(StartTime, StartTimeProperty, b);
//This does not work sadly, it can't convert string to DependancyObject

最佳答案

x:Name 赋给您的 TextBlock -

然后你可以像这样在代码后面做到这一点-

Binding b = new Binding("StartTime");
b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor,
typeof(Window), 1);
textBlock.SetBinding(TextBlock.TextProperty, b);

关于c# - 绑定(bind)到后面代码中的相关源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19793613/

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