gpt4 book ai didi

c# - WPF 绑定(bind)不接受任何更改

转载 作者:行者123 更新时间:2023-12-03 10:54:59 25 4
gpt4 key购买 nike

在我的 WPF 应用程序中,我有一些属性绑定(bind)到 XAML 对应项,但由于某种原因,当它们的值发生变化时不会设置它们。我已经实现了INotifyPropertyChanged界面以及设置我的DataContext对于这个 View 也是如此,它仍然没有进行任何更改。

我对这个 ViewModel 中的其他属性有同样的模式,这些属性确实有效,而其他属性则没有。

这是我当前代码的片段:

View 模型

public class TestViewModel : INotifyPropertyChanged
{
private string testString;

public TestViewModel()
{
.....
this.RunCommand = new RelayCommand(this.RunAction);
}

public string TestString
{
get
{
return this.testString;
}

set
{
this.testString = value;
this.OnPropertyChanged("TestString");
}
}

private void RunAction()
{
.....
this.testString = "Running.";
}
}

看法
<StatusBarItem>
<TextBlock Text="{Binding Path=TestString, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />
</StatusBarItem>

DataContext(在另一个 MainWindow 类的代码隐藏中设置)
var testViewModel = SimpleIoc.Default.GetInstance<TestViewModel>();
var testWindow = new TestWindow() { DataContext = testViewModel };
testingWindow.Show();

如果有帮助,这是使用 MVVM-Light 在类之间传递属性的多窗口应用程序的一部分。

最佳答案

您没有更改 TestString 的值,您正在分配一个命令来更改该值,但您似乎没有执行它。

this.RunCommand = new RelayCommand(this.RunAction);

将该命令绑定(bind)到某物或从某处手动执行它。

您还需要分配属性而不是字段
this.TestString = "Running.";

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

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