gpt4 book ai didi

WPF 简单绑定(bind)到对象属性

转载 作者:行者123 更新时间:2023-12-04 07:03:30 24 4
gpt4 key购买 nike

我在 wpf/xaml 中的绑定(bind)有一些问题。有这个简单的文件:

<Window x:Class="test.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBlock Height="21" Foreground="Black" Margin="74,98,84,0" Name="textBlock1" VerticalAlignment="Top" Text="{Binding MyText}" />
</Grid>
</Window>

我想将文本 block 的内容绑定(bind)到我的属性“MyText”。我的代码如下所示:
 public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

public string MyText
{
get { return "This is a test"; }
}
}

总而言之非常简单,但是当我启动文本 block 时没有内容 - 怎么会?

最佳答案

您的绑定(bind)中需要一个元素名称:

<Window ... x:Name="ThisWindow"...>

<TextBlock ... Text="{Binding MyText, ElementName=ThisWindow}" />

关于WPF 简单绑定(bind)到对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1462231/

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