gpt4 book ai didi

c# - WPF XAML 绑定(bind)不更新

转载 作者:太空狗 更新时间:2023-10-29 23:56:37 25 4
gpt4 key购买 nike

我有一个 WPF 项目,其中有 4 个 TextBlock。我想要的是通过 Binding 更改每个 TextBlockText

到目前为止,我有我的 XAML:

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="First" Text="{Binding FirstString}" Grid.Row="0"/>
<TextBlock x:Name="Second" Text="{Binding SecondString}" Grid.Row="1"/>
<TextBlock x:Name="Third" Text="{Binding ThirdString}" Grid.Row="2"/>
<TextBlock x:Name="Fourth" Text="{Binding FourthString}" Grid.Row="3"/>
</Grid>

在我的代码中我有:

public partial class MainWindow : Window
{
public string FirstString { get; set; }
public string SecondString { get; set; }
public string ThirdString { get; set; }
public string FourthString { get; set; }

public MainWindow()
{
InitializeComponent();

FirstString = "First";
SecondString = "Second";
ThirdString= "Third";
FourthString= "Fourth";
}
}

但是 Binding 根本不起作用。我做错了什么吗?

编辑:按照 Chris Mantle 在评论中的建议查看调试输出(我必须为绑定(bind)启用警告)后,我收到以下错误:

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=FirstString; DataItem=null; target element is 'TextBlock' (Name='First'); target property is 'Text' (type 'String')

最佳答案

有几处是不正确的。 Binding 标记将查看控件的 DataContext 属性中的对象。除非另有说明,否则此属性从声明父级继承 DataContext。对于 Window 控件,这是开箱即用的 null

这个问题有两种选择。您可以在代码隐藏或 XAML 中显式设置 DataContext

// In XAML
<Window DataContext={Binding RelativeSource={RelativeSource Self}}>

or

// In the code-behind
DataContext = this;

另一个问题是绑定(bind)是在初始化时应用的。最初,您的属性是空的。在 InitializeComponent 阶段之后,控件将“绑定(bind)”到属性(为空)。之后设置属性时,控件无法知道它已更改。有两种机制可以做到这一点。在控件级别,您可以将这些属性设置为 DependencyProperty或实现 INotifyPropertyChanged接口(interface)并提出更改。如果你想走 INPC 路线,你可以这样实现你的属性和窗口:

public partial class MainWindow : INotifyPropertyChanged
{
private string firstString;
private string secondString;
private string thirdString;
private string fourthString;

public string FirstString
{
get { return firstString; }
set
{
firstString = value;
RaisePropertyChanged("FirstString");
}
}

public string SecondString
{
get { return secondString; }
set
{
secondString = value;
RaisePropertyChanged("SecondString");
}
}

public string ThirdString
{
get { return thirdString; }
set
{
thirdString = value;
RaisePropertyChanged("ThirdString");
}
}

public string FourthString
{
get { return fourthString; }
set
{
fourthString = value;
RaisePropertyChanged("FourthString");
}
}

public MainWindow()
{
DataContext = this;
InitializeComponent();

FirstString = "First";
SecondString = "Second";
ThirdString = "Third";
FourthString = "Fourth";
}

public event PropertyChangedEventHandler PropertyChanged = delegate { };

private void RaisePropertyChanged(string propertyName)
{
var handlers = PropertyChanged;

handlers(this, new PropertyChangedEventArgs(propertyName));
}
}

关于c# - WPF XAML 绑定(bind)不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18060268/

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