简体   繁体   English

绑定不响应PropertyChanged

[英]Binding doesn't respond to PropertyChanged

I have a model and a view-model for a Window and I'm stuck with view. 我有一个模型和一个窗口的视图模型,我被视图困住了。
The view has a control that has the following Content: 视图的控件具有以下内容:

<TextBlock Text="{Binding Title}" [...] />

The data-context is the view-model, which has a Title property of type string. 数据上下文是视图模型,其Title属性为string类型。

The issue is that even when the value changes (and I can see it does through the debugger) and PropertyChanged is called for the Title , the TextBlock doesn't change (it stays empty). 问题是,即使值发生变化(我可以通过调试器看到它​​)并且为Title调用PropertyChangedTextBlock也不会更改(它保持为空)。

To confirm this I've actually called an update method on every mouse press. 为了确认这一点,我实际上每次按下鼠标都会调用更新方法。

I've heard sometimes the binding in TextBlock s are bugged so I've also tried a Label but it didn't work. 我听说有时TextBlock的绑定被窃听,所以我也试过了一个Label,但它没有用。 Any suggestions? 有什么建议么? If there's any code you want to see please tell me in the comments (the whole thing is pretty big). 如果你想看到任何代码,请在评论中告诉我(整个事情都很大)。

ViewModel 视图模型
Code-behind for the View. View的代码隐藏。

To be clearer: Window is my model, and not WPF's Window! 更清楚: Window是我的模型,而不是WPF的窗口!

Your _window.Title is string.Empty . 你的_window.Title是string.Empty。 Assign some value to it. 为它分配一些值。 Like 喜欢

public WindowViewModel()
{
    Window = new Window(){Title="abc"};
}

Update I tried your code but it didnt work for me I did one change and it worked the change is 更新我尝试了你的代码,但它没有为我工作我做了一个改变,它的工作变化是

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

It would work if you instantiate your WindowViewModel class 如果您实例化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: 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>

您可以将属性名称(在您的情况下为“标题”)传递给OnPropertyChanged方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM