绑定未响应PropertyChanged。

3
我有一个窗口的模型和视图模型,但是我在处理视图时遇到了困难。
该视图包含一个控件,其内容如下:
<TextBlock Text="{Binding Title}" [...] />

数据上下文是视图模型,它具有字符串类型的Title属性。
问题在于,即使值发生改变(我可以通过调试器看到),并且为Title调用了PropertyChanged,TextBlock也不会改变(它保持为空)。
为了确认这一点,我实际上在每次鼠标按下时都调用了更新方法。
我听说过有时候TextBlock中的绑定存在缺陷,所以我也尝试了Label,但它没有起作用。有什么建议吗?如果您想要查看任何代码,请在评论中告诉我(整个代码非常大)。 ViewModel 视图的代码后台 更清晰地说,Window是我的模型,而不是WPF的Window!

你确定没有绑定错误吗?你有检查过“输出”窗口吗? - Anand Murali
@AnandMurali 是的,我总是检查它。 - MasterMastic
@NahumLitvin 我在底部添加了ViewModel的链接。XAML位于顶部(这确实是全部内容)。 - MasterMastic
不是这样的。你可能已经搞砸了DataContext。(你的cs没问题) - Nahum
真的吗?IntPtr HandleWindow这些术语占据了我脑海中一个叫做“Win32 UI”的盒子。 - Ritch Melton
显示剩余10条评论
3个回答

1
您的_window.Title是string.Empty。赋值一些值给它。例如:
public WindowViewModel()
{
    Window = new Window(){Title="abc"};
}

更新 我尝试了您的代码,但它对我不起作用。我进行了一项更改,然后它起作用了。更改是:

public WindowEntryView()
{
    DataContext = new WindowViewModel();
    InitializeComponent();
}

我无法这样做。标题的设置器需要一个实际窗口句柄才能工作。它更像是一个方法而不是一个变量(没有私有字段)。此外,那会如何帮助呢? - MasterMastic
在代码中,WindowViewModel 没有被实例化,对吗?还是我漏掉了什么。 - Anand Murali
是的,你说得对(我一直在尝试修复它)。但即使我已经修复了它,它仍然没有更新。 - MasterMastic
@Ken,你能否将get { return _window.Title; }替换为get { return "abc"; },以验证你的绑定是否正确? - yo chauhan
@ethicallogics 哦,我明白你的意思了。绑定是有效的,但只有一次。我知道这是因为如果没有句柄,模型会返回一个特定的字符串。问题正是标题所说的...它不响应任何PropertyChanged通知。所以我创建视图并显示“无句柄”字符串。然后设置句柄,但它仍然显示“无句柄”。 - MasterMastic
我尝试了你的代码,但它没有运行,并且在输出窗口中没有错误。依赖属性出了问题。我通过实例化直接设置了数据上下文,这样能够正常工作。我已经更新了答案。 - yo chauhan

1
"我希望您可以将WindowViewModel类实例化,这样就可以正常运行。"
"

CodeBehind:

" -> "

代码后台:

"
public partial class WindowEntryView
{
    public static readonly DependencyProperty WindowViewModelProperty;
    public WindowViewModel WindowViewModel
    {
        get { return (WindowViewModel)GetValue(WindowViewModelProperty); }
        set
        {
            SetValue(WindowViewModelProperty, value);
            value.Refresh(); // I've putted this out of desperation to see if it would help.. it didn't.
        }
    }

    static WindowEntryView()
    {
        PropertyMetadata metadata = new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender); // Another attempt with no success.
        WindowViewModelProperty = DependencyProperty.Register("WindowViewModel", typeof(WindowViewModel), typeof(WindowEntryView), metadata);
    }

    public WindowEntryView()
    {
        //WindowViewModel = new WindowViewModel(19860522); This is the only attempt that made the label show something, but it didn't update later of course.

        InitializeComponent();

        WindowViewModel = new WpfApplication3.WindowViewModel() { Title = "Check" };
        DataContext = WindowViewModel;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        WindowViewModel.Title = DateTime.Now.ToString();
    }
}

XAML:

<Grid>
    <TextBlock Text="{Binding Title}" VerticalAlignment="Top"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="116,131,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>

仔细看源代码。他试图以一种复杂的方式实现这个目标。 - Ritch Melton
@AnandMurali 我的 XAML 是用来做什么的?请具体说明一下。 - MasterMastic
我已经更新了答案,包括我的示例XAML。它在我的端上运行良好,所以我认为您的XAML可能出错了。 - Anand Murali
@AnandMurali 噢,我以前尝试过类似的东西。这对第一次会起作用,但不会更新。 - MasterMastic
更新它,点击按钮并检查。TextBlock将被更新为当前时间。 - Anand Murali
让我们在聊天中继续这个讨论。 (Let us continue this discussion in chat.) - Anand Murali

0

请将属性名称,在您的情况下为“Title”,传递给OnPropertyChanged方法。


尝试过了,但没成功。虽然这并不奇怪,因为“刷新”有这段代码。 - MasterMastic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接