gpt4 book ai didi

c# - 将绑定(bind)从 C# 转换为 XAML

转载 作者:行者123 更新时间:2023-11-30 13:42:17 26 4
gpt4 key购买 nike

我无法在 XAML 中使用此绑定(bind)。

在 C# 中绑定(bind)有效:

public partial class myControl : UserControl
{
// get singleton instance
InfoPool Info = InfoPool.Info;

public myControl()
{
InitializeComponent();

// Test Binding
Binding bind = new Binding();
bind.Source = this.Info;
bind.Path = new PropertyPath("Time");
txtInfoTime.SetBinding(TextBlock.TextProperty, bind);
}
}

在 XAML 中绑定(bind)不是:

<TextBlock x:Name="txtInfoTime" Text="{Binding Path=Time, Source=Info}" />

Path 和 Source 是一样的,那我哪里错了?

谢谢罗布

最佳答案

您无法将其转换为具有完全相同属性的 XAML,因为无法直接引用 this.Info。但是,您可以通过设置 RelativeSource 来获得相同的结果:

<TextBlock x:Name="txtInfoTime" Text="{Binding Path=Info.Time, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:myControl}}}" />

关于c# - 将绑定(bind)从 C# 转换为 XAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3512669/

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